|
Easily Avoid a Common Data Structure Error
Published: February 25, 2009
by Ted Holt
A shop with which I'm acquainted had an undesirable experience recently. A program that, to my understanding, had been working properly, ended abnormally. The error could have easily been avoided, had the programmer followed one simple rule of thumb.
Take a look at the following program fragment.
D MyData ds
D OneField 4a
D TwoField 7p 0
D RedField 3s 0
D BlueField 5u 0
D
D Number s 15p 5
/free
Number = TwoField;
If the calculation is the first executable statement in the program, what is the new value of Number? The answer is that Number never gets a new value. TwoField has an initial value of hexadecimal 40404040, which is not a valid packed decimal value. The program receives message MCH1202 and that's the end of that.
Suppose the first executable statement is this one:
Number = BlueField;
What's the value of Number after this assignment? That's right! Sixteen thousand, four hundred forty-eight, the decimal equivalent of hex 4040. In this case, the program runs, but has a logic error.
Such errors won't occur if you habitually initialize data structures. That is, add the INZ keyword to the first line.
D MyData ds inz
The best way to debug a program is not to put bugs into it in the first place. Little rules of thumb, like "always initialize data structures," move you toward that goal.
Post this story to del.icio.us
Post this story to Digg
Post this story to Slashdot
|