TechTip: Coding Free-Format RPG IV (and Overcoming Old Habits)

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

The debate over whether to code new RPG programs in the free-format style has just about ended. Most agree that this style is preferable, in one way or another. With this shift in coding style comes a not-so-fun shift in work habits.

The free-format style of RPG has many fewer operations than its fixed-format sibling. At V5R3, there are 59 free-format opcodes and 112 fixed-format ops. Many of the commonly used opcodes of the "fixed" world are just not there in free-format. Some of these we probably won't miss very much, such as the four zone moves (MHHZO, etc.), the math ops (ADD, SUB, MULT, DIV, and MVR), the numeric moves (Z-ADD and Z-SUB), and the indicator set duo (SETON and SETOFF).

Some programmers will agonize over the loss of the "compare and branch" group (CABxx) and GOTO/TAG. I don't mean to sound critical, but programs using these operations are not well-structured. These opcodes were needed in the era that preceded structured loop operations. Perhaps you began coding when CABxx and GOTO were the only way to conditionally loop, but it's time to overcome this old habit. In free-format, you use DOx and FOR groups with loop interrupters LEAVE and ITER.

Many fixed-format opcodes have become built-in functions (BIFs). The BIF method becomes the only way to do the work in free-format. The many BIF alternatives perform the same functions as the old opcodes did. The following are a couple of examples. The first uses table lookup:

                                                         =
     C       TEST    LOOKUP   TAB1    TAB2               70
     C               IF       *IN70
     C               ----


The equivalent in free-format style would be the following:

      /free
       If %tlookup(Test:Tab1:Tab2);
         ----


Here's another example using substringing:

     C       4       SUBST   NAME:5   FIRST


The equivalent in free-format style would be this:


      /free
       First = %subst(Name:5:4);


To overcome the habit of using these familiar opcodes, some "school" time is needed to learn the BIFs that replace opcodes. Most are very straightforward and easy to learn. Since the BIFs can be used in an expression, you can practice using them in fixed-format by placing them in the extended factor 2 area and using an Eval opcode. This may be a good transition method for you before going exclusively to free-format.

By far, the most common habit—one that we must overcome—is the use of the MOVEx operations. These opcodes are used extensively in fixed-format RPG but are not available in free-format. I won't try to oversimplify the task of finding a best alternative for these operations. Here are a few suggestions, however.

The MOVE operation can do many different things, depending on the factors used. IBM calls this a "loaded" operation. The compiler must determine what function to perform based on the data types of the factors, lengths of the factors, and options specified in factor 1. Since there is no MOVE opcode in free-format, we must decide what alternative is appropriate for the function needed. To convert a field from character to numeric, we can use the %dec or the %int BIF. To convert a field from numeric to character, we can use the %char or the %editc (with 'x' code) BIF. To move a character field to another character field with right justification, the EVALR opcode may work. To move characters to a receiving field that is longer than the source field (and padding is not desired), the %subst or the %replace BIF can do the job (albeit more parameters and keystrokes are needed).

Here's an example:

     D Short        S      2       Inz('AA')
     D Long         S      6       Inz('WWLLYY')

     C              Move    Short      Long

* Long is now 'WWLLAA'


Here's the equivalent in free-format:

      /free
       Long=%replace(Short:Long:%len(Long)-
           %len(Short)+1);


Another method uses two statements but is a little less cumbersome:

       Long = Short + Long;
       EvalR Long = Long + Short;


The MOVEL operation isn't quite as hard to handle. Mostly, the EVAL operation or simply an assignment statement works OK. For character moves that have a receiving field longer than the source field, the %replace BIF can easily do the job. Here's an example:

     D Short        S      2       Inz('AA')
     D Long         S      6       Inz('WWLLYY')

     C              MoveL   Short      Long

* Long is now 'AALLYY'


This is the free-format equivalent:

      /free
       Long = %replace(Short:Long);


The MOVEA operation has been addressed in a prior technical tip, but any way you look at it, there is work to do in analyzing the task (previously done using MoveA) and deciding on a best alternative.

We are creatures of habit, and free-format RPG has forced us to move out of our comfort zone. The result of shaking these old habits and acquiring new ones is better, more readable programs.

Jim Martin, the author of Free-Format RPG IV, is a corporate technical instructor at Jack Henry & Associates in Monett, Missouri. He is a veteran of RPG programming, beginning in 1967 with a position at IBM as a systems engineer and later as a staff programmer at the Rochester systems programming lab. He is a speaker at COMMON and local midrange user group meetings and conferences. For eight years, he was at Lakeview Technology as an AS/400 and RPG instructor. He can be reached by email at This email address is being protected from spambots. You need JavaScript enabled to view it..


BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$