A friend of mine at work gave me the idea of posting coding tips and techniques, or "cheats" as she put it. ;) I liked the idea, so my first will be an RPG ILE tip for the System i: using a subprocedure as a indicator.
One of the great things about subprocedures in RPG ILE--and there are many--is being able to use its return value in an expression. This technique allows for some handsome code. In this post, I will demonstrate how to define and use a subprocedure which returns an indicator type variable.
First, the prototype. "Things_R_Good" is an example of a validation routine. Notice the "N" in the data type column, and the absence of any length.
//-----------------------------------------------------
// Verify that things are good
//-----------------------------------------------------
D Things_R_Good PR N
Now, the subprocedure. The way this works is that normal validation will occur, and when there are any issues, the local variable "got_error" is set on. As we know that things are NOT good if we got an error, we return the opposite of "got_error" at the end of the subproc.
//-----------------------------------------------------
// Things_R_Good - Verify that things are good
//-----------------------------------------------------
PThings_R_Good B
D PI N
// Local variables
D got_error S N
/Free
// Do the validation
...
Return not got_error;
/End-free
PThings_R_Good E
What happens is when got_error = *ON, we return *OFF, and vice versa. Then we use the subprocedure in the mainline, or other subprocedure, as follows.
// If everything is good, continue processing.
If Things_R_Good();
...
EndIf;
Notice that because the subprocedure was named as it was the code becomes extremely readable: "if things are good".
- Buck