24
Tue, Dec
1 New Articles

Programmer's Toolbox: DO Groups in CL Programs

CL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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 */ 
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: