shareware

The Most Common Programming Bug.

Programmers not initializing fields when a record is not found is the most common programming bug. I have seen million dollar packages do nothing or abort on records not found. Exception handler routines do not handle record not found well either. This problem is not limited to the i5 and RPG, I have seen it in COBOL, Visual basic, and Java too. Example:

C	Customer	Chain	CustMast		54
C* not found, do nothing

Lets say you were trying to accumulate totals for accounts receivable, and customer 50 was deleted. That means customer 49 accounts receivable amount would be added in twice since it was not initialized to zero. And customer 49 name would be next to customer 50 on the report. A better solution, Example:

C	Customer	Chain	CustMast		54
C			If	*In54
C			Move	*blanks		Name
C			Move	*zeros		ArAmt
C			End

This solution would require you to initialize every field you expected to find individually. And if you changed the program or file layout later, you would have to add new fields. RPGLE offers some easy solutions to this problem, Example:

FCustMast	IF A E		K Disk	PreFix(Cm)
D CustMastDs	DS			ExtName(CustMast) PreFix(Cm) Inz
C			Clear			CustMastDs
C			Move	Customer	CmCustomer
C	CmCustomer      Chain	CustMast			54
C			If	not %found
C			Move	Name		CmName
C			(...other required fields)
C			Write	rCustMast
C			End

The above clear operation initializes every field in the Customer Master Data Structure to blanks or zeros.  Clear will not  work on input only record formats.  To clear all fields in a files record format requires you to defined record format as a data structure like above or define the file as both input and output.  Clearing a input only record format will give you a warning error when compiled.  This warning message says the clear operation was ignored.  The reason is that many of the input fields are dropped (see 7031 compile messages).  I consider this a bug in the RPG compiler. 

Putting every file in its own data structure requires the field in each file to have a unique name, that is why the Prefix(Cm) is used on both the "F" file spec and "D" data structure definition. If your file/fields are already unique, the you can leave off the prefix.

The Inz initializes every field at program load time. This ensures that you will not get a data decimal error if you try to use a customer master field before the clear is executed.

Notice the Move is required if you may want to write a new customer record on a not found condition. The sequence of events to add a record is Clear all fields for the file, load key fields, chain to file, if not found, load non key fields, and write the record.