Problem solve Get help with specific problems with your technologies, process and projects.

Free form RPG: Are you /FREE?

System i (AS/400) programming expert Paul Tuohy attended a recent Common User Discussion Social (CUDS) recently where the conversation centred on free form RPG. Paul shares some of the points discussed, highlighting the highs and lows of using free form RPG and some of the conclusions that were reached.

One of the things that I enjoy most when speaking at conferences is networking with other programmers. I end up...

having many fascinating conversations over cups of coffee (and other beverages). One evening at Common User Discussion Social (CUDS), at the recent Common conference in Miami, one such conversation centred on free form RPG.

I'd like to share with you, some of the points discussed, highlighting the highs and lows of using free form RPG and some of the conclusions that were reached.

A quick reminder

Free form RPG was introduced in V5R1 of OS/400. For those of you who have not tried it yet, these are the rules for coding in free form.

  • Free form code is placed between the /FREE and /END-FREE compiler directives.

  • The structure of an operation is the operation code followed by Factors 1, 2, and the Result Field

  • Each statement must end with a semicolon (;)

  • Operands are no longer limited to 14 characters, especially for operations that used Factor 1

  • No blanks are allowed between an operation code and extenders

  • Only one operation code may be coded on a line

  • Comments are delimited by //. Comments may be placed at the end of any free-form statement (after the ;)

  • Some operation codes are not currently supported (more in a moment)

  • Some operation codes (such as CALLP and EVAL) are optional, except where an extender is needed

Legibility

Perhaps the biggest benefit to be derived from free form is one of legibility. In free form you may use the positioning of the code to help clarify the structure.

Figure 1 shows a comparison between a snippet of code written using extended Factor 2 and the exact same code in free form. The use of free form makes it easy to see the structure of the nested IFs and the IF/ELSE. Actually, the code shown in Figure 1 was converted to free form using an option in the Remote System Explorer (RSE) LPEX editor in Websphere Development Studio Client (WDSC) – Source/Convert All to Free Form.

 

Increased legibility is also provided by the fact that you have more space on a line for code. This is especially useful when you start using qualified data structures, which lends to longer names on a line since field names must be qualified with the data structure name.

Of course, it is possible to write hard to read (if not illegible) code in free form but that can be said of whatever coding structure you use. I can write code as badly in free form as I do in fixed form but, on the other hand, I can write code that is a lot easier to read in free form than it is in fixed form.

Unsupported operation codes

Not all operation codes are supported in free form. Some operation codes have been replaced by Built-in Functions (BIFs) (e.g. %SUBST, %OCCUR, %XLATE) but other operation codes do not have a direct equivalent. The more obvious of these are the standard ADD, SUB, DIV, MULT, MOVE and MOVEL operation codes. Just as with extended Factor 2, these operation codes may be emulated with the EVAL operation but you must remember that EVAL is not as forgiving as the fixed format operation codes.

Numeric overflow is not tolerated by EVAL so you will receive a run-time error if you fill a numeric field as opposed to the value wrapping around to zero.

There is no direct equivalent of the MOVE operations; you must use BIFs (%CHAR, %DEC, %DATE, *INT) is you need to move data between fields with different data types and you may need to use %SUBST if you need to move data between fields of different lengths. A lot of people see this as a problem but I actually see it as an advantage in that it leads to self-documenting code. Compare the two lines of code shown in Figure 2. In the fixed format line you must be familiar with the definition of the two fields to understand what the MOVEL operation is achieving whereas the free form equivalent is self explanatory; it may not be as easy to code but it is easier to understand when you read it.

Figure 1 shows a comparison between a snippet of code written using extended Factor 2 and the exact same code in free form. The use of free form makes it easy to see the structure of the nested IFs and the IF/ELSE. Actually, the code shown in Figure 1 was converted to free form using an option in the Remote System Explorer (RSE) LPEX editor in Websphere Development Studio Client (WDSC) – Source/Convert All to Free Form.

 

It is now possible to have a program that consists of fixed format, extended Factor 2 and free form RPG -- not a pretty site and something to be avoided if possible.

Extended Factor 2 operations will convert directly to free form but you need a third-party tool (or write one yourself) if you want to convert the unsupported operation codes to free form.

Only FREE

shows the traditional code for defining a key list to be used with a file where the key consists of a number of fields.

shows how a key list may be emulated in the D specs. An externally defined data structure is defined using the optional parameter of *KEY on the EXTNAME key word to indicate that only key fields from the external file are to be included in the data structure. This data structure is then used as an argument for the %KDS BIF on the CHAIN operation. The %KDS BIF may only be used in free form. This is slightly better than a key list in that the %KDS keyword makes it clear that a data structure is being used to define the key.

 

But Figure 7 shows the preferred way to access a record using multiple key fields. Simply provide the list of key fields to be used directly on the operation code. You may even use a literal and/or an expression as one of the key fields!

 

Another new language feature that is geared towards free form is the XML-INTO operation code (introduced in V5R4). You must use free form if you want to use either the E or H extender with XML-INTO since the length of the operation code and the extender is eleven characters as opposed to the ten allowed for the operation code in fixed form and extended Factor 2.

Frustration

It can be frustrating getting to grips with indenting, learning all those BIFS that you need to use to replace traditional familiar operation codes and remembering to put the semi colon at the end of the line.

A lot of this frustration is relieved if you are using RSE; the editor automatically indents when you enter a structured operation code, provides Content Assist to prompt BIFS (enter a BIF name or part of a BIF name and press Space+Enter) and tells you if you forgot the semi colon.

Using the right tools always makes the job easier.

Be free

At the end of the conversation at CUDS the overall conclusion was that the benefits of free form far outweigh the effort it takes to get there.

Free form RPG takes a little getting used to. It is alien and does not feel like RPG. It takes a while for free form to have that feeling of familiarity.

But you should become familiar with it. I have a funny feeling that the future of RPG is making free form a necessity.

-----------------------------------
About the author: Paul Tuohy is CEO of ComCon, an iSeries consulting company. He is the author of "Re-engineering RPG Legacy Applications", "The Programmers Guide to iSeries Navigator" and the self teach course "iSeries Navigator for Programmers". He is also an award winning speaker who speaks regularly at US Common conferences and the renowned RPG World conferences.
 

This was last published in October 2006

Dig Deeper on RPG iSeries programming

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchEnterpriseLinux

SearchDataCenter

Close