Most people know how to code a DO group in CL programs, but let me give you a few tips that I use. Making a mistake on a DO group can be very expensive. The most hated messages in the world of CL programming are the ones that say you have too many or not enough ENDDOs. The compiler provides no help at all with the DO groups (unlike RPG, which numbers them with 1s and 2s in a column on the listing). You can spend a lot of time checking your code for the missing or extra ENDDO.
Most of the time, I use a DO group associated with an IF command. When you code an IF command, you can enter almost any command on the THEN parameter. Over the years, I have moved in the direction of almost always coding a DO rather than a specific command. An example of my code might look like this:
IF (&LIB *EQ 'LIB1') DO /* Library 1 */ ENDDO /* Library 1 */
As a matter of personal preference, I use the shorthand form on the IF (no keywords). This method helps me in that I can make a longer comment on the same line for my DO group. I also prefer the English-like '*EQ' instead of the mathematical '='. But these are just my personal preferences.
As soon as I enter the DO, I add the ENDDO. This helps avoid the problem of missing or extra ENDDOs. I put my comments in lowercase and my code in uppercase. Flipping back and forth costs something, but, for me, it's worth the effort. I would estimate that I read three lines of code for each line of code I write. I may not read each line three times, but I read a lot of my code. My eye is trained to read uppercase as code and lowercase as comments, message text, and so forth.
I don't indent when I code a DO group (you really can't if you use the prompter). I find the little comments are a big help in identifying DO groups.
Speaking of comments, in most cases I have moved away from commenting the individual statements (except for DO/ENDDO). I prefer a block of comments like this:
/* Use special handling for Lib 1 */ IF (&LIB *EQ 'LIB1') DO /* Library 1 */ ENDDO /* Library 1 */
Most of the time when I am looking at code, I am scanning for a chunk of code that I am really interested in. I find that the blocks of comments help break up the code and let me more easily find the area to focus on. I find that too many comments within the code lines distract from my focus. You can also overdo the comment boxes. I try to break up the code but not interrupt it.
Unlike RPG, the how of CL coding is normally clear just by looking at the statements (the keywords help a lot). I tend to write more comments about what I am doing or why I am doing something.
There are two reasons I prefer the DO group even if I have only a single command to enter:
1) I like the command names in a nice, neat column (you can tell I'm an RPG programmer). My eye is trained to scan these quite rapidly looking for what I want.
2) If I think I can get by without a DO group, half the time I wind up wanting to add another command that must be part of the same IF, and I change it anyway.
Compound IF statements are not my favorites. Most of my compound IFs look like this:
IF ((&LIB *EQ 'LIB1') *OR + (&LIB *EQ 'LIB2')) DO /* Lib 1 or 2 */
You can use the same technique with *AND.
The *OR function is not one of my favorites either. You can get in trouble with *OR, as in the following:
/* Bad example */ IF ((&LIB *NE 'LIB1') *OR + (&LIB *NE 'LIB2')) DO /* Not 1 or 2 */
Regardless of what value exists for &LIB, the code always executes the DO. Unfortunately, the compiler does not tell you that you are doing a dumb thing. When you use *OR, stay away from *NE.
I don't use a lot of compound IF statements. Sometimes I just code another IF with another DO group. You don't have to worry about performance unless you are iterating on the same code hundreds of thousands of times. You can combine *AND and *OR in the same compound statement. If you do, the system interprets the *AND first unless you use parentheses. It's not simple, and I don't have much need for it, so I prefer to just code another IF.
Following an IF, you can code an ELSE and even another DO group. I don't use ELSE. I prefer to put the negative logic in another IF command along with another DO, like this:
IF (&LIB *EQ 'LIB1') DO /* Library 1 */ ENDDO /* Library 1 */ IF (&LIB *NE 'LIB1') DO /* Not Library 1 */ ENDDO /* Not Library 1 */
Once again, you rarely have to worry about performance. While this is my personal preference, because I think it documents better, I think you should do what you are comfortable with.
Despite my techniques, I occasionally get the hated messages about missing or extra ENDDOs. If it's a small program, I just look at the code. If the program gets big, I bring out a tool to help solve the problem.
A long time ago, I wrote the Display CLP DO Groups (DSPCLPDO) command as a TAA Tool in QUSRTOOL (also in the TAA Productivity Tools). It's still there (until V3R6) and still very helpful. The output appears in 1; the DO groups are indicated by 1s and 2s in columns on the left (just like an RPG listing). The begin (B) and end (E) of each group are also shown.
A long time ago, I wrote the Display CLP DO Groups (DSPCLPDO) command as a TAA Tool in QUSRTOOL (also in the TAA Productivity Tools). It's still there (until V3R6) and still very helpful. The output appears in Figure 1; the DO groups are indicated by 1s and 2s in columns on the left (just like an RPG listing). The begin (B) and end (E) of each group are also shown.
The command I am using now is the Print CLP DO Groups (PRTCLPDO) command, which is part of the TAA Productivity Tools (not in QUSRTOOL). It does an indentation and shows columns of asterisks to provide a better picture of how the code is arranged. This tool can also be helpful when you are just studying the logic of the program. (See 2.)
The command I am using now is the Print CLP DO Groups (PRTCLPDO) command, which is part of the TAA Productivity Tools (not in QUSRTOOL). It does an indentation and shows columns of asterisks to provide a better picture of how the code is arranged. This tool can also be helpful when you are just studying the logic of the program. (See Figure 2.)
Some programmers will tell you that all GOTOs should be eliminated. Unfortunately, CL programs do not support Do While or Do Until. All you have is DO. So you need GOTO.
Personally, I think a well-placed GOTO is a blessing in trying to understand code. I don't like to code a DO group that goes on for several pages. Sure, I make errors because of GOTO, but I also make errors with too much nesting or a DO group that's too long.
While my code doesn't always look simple, I think trying to keep it simple works the best. There is a significant advantage in finding a technique or style that works and then sticking with it.
DO groups can be very simple, and, if you run into problems, the tools can help you out.
Jim Sloan, president of Jim Sloan, Inc., is a software vendor and consultant. A retired IBMer, Sloan was a software planner on the S/38 when it began as a piece of paper. He also worked on the planning and early releases of the AS/400. In addition, Sloan wrote the TAA tools that exist in QUSRTOOL and is now the owner of the TAA Productivity Tools product. He has been a speaker at COMMON for many years.
Programmer's Toolbox: DO Groups in CL Programs
Obtaining and Installing the DSPCLPDO and PRTCLPDO Commands
The Display CLP DOGroups (DSPCLPDO) command can be found in the TAA Tools in the QUSRTOOL library, which is shipped with every AS/400 through V3R1M0. The TAA Tools of QUSRTOOL will no longer be shipped, starting with V3R6.
The QUSRTOOL library must be unpackaged, and each tool within the library must be created. If you haven't unpackaged the QUSRTOOL library, see source member AAAMAP in source file QATTINFO, library QUSRTOOL for instructions. If you haven't created all the tools or at least the DSPCLPDO command, see source member CRTTAATOOL in source file QATTINFO, library QUSRTOOL for instructions.
The Print CLP DO Groups (PRTCLPDO) command can be found in TAA Productivity Tools, a licensed product from Jim Sloan, Inc. TAA Productivity Tools ships with both source and object code already created. There's no need to create the tools as with the QUSRTOOL library (the PRTCLPDO command will already exist in library TAATOOL).
For more information about TAA Productivity Tools, contact
Jim Sloan, Inc.
c/o Barsa Consulting Group, Inc.
914-939-6100
Programmer's Toolbox: DO Groups in CL Programs
Figure 1: DSPCLPDO Spooled File
File-QCLSRC Library-SLOANT Member-TAANAMAC5 DO Grp Err Seq Nbr Statement 23.00 /********************************************/ 24.00 /* */ 25.00 /* Ensure library exists. */ 26.00 /* */ 27.00 /********************************************/ B 1 28.00 IF (%SST(&LIB 1 1) *NE '*') DO /* Check name */ 1 29.00 CHKOBJ OBJ(&LIB) OBJTYPE(*LIB) B 2 30.00 MONMSG MSGID(CPF9801) EXEC(DO) /* No library */ 2 31.00 RCVMSG MSGTYPE(*EXCP) 2 32.00 SNDESCMSG MSG('Library ' *CAT &LIB *TCAT + 2 33.00 ' does not exist') E 2 34.00 ENDDO /* No library */ E 1 35.00 ENDDO /* Check name */ 36.00 /********************************************/ 37.00 /* */ 38.00 /* Ensure user exists. */ 39.00 /* */ 40.00 /********************************************/ B 1 41.00 IF (&USER *NE '*PUBLIC') DO /* Not *PUBLIC */ 1 42.00 CHKOBJ OBJ(&USER) OBJTYPE(*USRPRF) B 2 43.00 MONMSG MSGID(CPF9801) EXEC(DO) /* No library */ 2 44.00 RCVMSG MSGTYPE(*EXCP) 2 45.00 SNDESCMSG MSG('User profile ' *CAT &USER *TCAT + 2 46.00 ' does not exist') E 2 47.00 ENDDO /* No library */ E 1 48.00 ENDDO /* Not *PUBLIC */
Programmer's Toolbox: DO Groups in CL Programs
Figure 2: PRTCLPDO Spooled File
2/10/96 7:56:07 TAASYS4 PRTCLPDO - TAA - Print CLP Do Groups Member-TAANAMAC5 Page 1 File-QCLSRC Library-SLOANT DSPSPLF-*NO Seq ErrNte Change date 23.00 /********************************************/ 24.00 /* */ 25.00 /* Ensure library exists. */ 26.00 /* */ 27.00 /********************************************/ 28.00 IF (%SST(&LIB 1 1) *NE '*') DO /* Check name */ 29.00 * CHKOBJ OBJ(&LIB) OBJTYPE(*LIB) 30.00 * MONMSG MSGID(CPF9801) EXEC(DO) /* No library */ 31.00 * * RCVMSG MSGTYPE(*EXCP) 32.00 * * SNDESCMSG MSG('Library ' *CAT &LIB *TCAT + 33.00 * * ' does not exist') 34.00 * ENDDO /* No library */ 35.00 ENDDO /* Check name */ 36.00 /********************************************/ 37.00 /* */ 38.00 /* Ensure user exists. */ 39.00 /* */ 40.00 /********************************************/ 41.00 IF (&USER *NE '*PUBLIC') DO /* Not *PUBLIC */ 42.00 * CHKOBJ OBJ(&USER) OBJTYPE(*USRPRF) 43.00 * MONMSG MSGID(CPF9801) EXEC(DO) /* No library */ 44.00 * * RCVMSG MSGTYPE(*EXCP) 45.00 * * SNDESCMSG MSG('User profile ' *CAT &USER *TCAT + 46.00 * * ' does not exist') 47.00 * ENDDO /* No library */ 48.00 ENDDO /* Not *PUBLIC */
LATEST COMMENTS
MC Press Online