RPG Evolution, Modernization and Refactoring – Starting with RPG 2 and the dreaded Logic Cycle

RPG (Report Program Generator) is an old Language

Fifty years ago, the RPG language looked very different to how it looks today. Fifity years ago, RPG was written using an internal computer *logic flow* called the LOGIC CYCLE.

The logic cycle is a series of stages that any program goes through - in very simple form it says READ IN DATA, then DO SOME LOGIC, then PRINT SOME STUFF, then GO BACK TO START OF LOOP.

The logic cycle can be simply viewed like this:

Rpg logic cycle simple

But, in reality, the cycle is a very complex set of logic instructions

Early RPG programs were written to use this cycle, they were written in column based format where any given character in a certain column would change how that line of code worked. They used numeric indicators (01-99) as switches to denote if things had happened.

Rpg2 logic cycle

Indicators?

For example, nowadays we might say something like "if an order exists then set variable ORDER_EXISTS to *ON". Later in the code we can say "if ORDER_EXISTS then do this order logic, otherwise tell the user that the order does not exist"

Back in the old programming days would say something similar "if an order exists then set variable turn indicator 22 *ON". Later in the code we would then say "if indicator 22 then do this order logic, otherwise tell the user that the order does not exist"

Column Based Code?

Column based code is the old RPG layout - which was popular from the 1970's to the mid 1990's.

RPG has been free format (like java script if you know it) from the late 1990's

Let's see what the 'RPG Logic Cycle' offers

The first and last time the program goes through the cycle is slightly different from all the in-between cycle runs:

Before the first record is read the first time through the cycle, the program resolves any parameters passed to it, writes the records conditioned by the First Page (1P) indicator, does file and data initialization, and processes any heading or detail output operations. This may also include printing heading lines.

During the last time a program goes through the cycle, when no more records are available, the LR (last record) indicator and L1 through L9 (control level) indicators are set on, and file and data area cleanup is done.

At the most basic level think of the logic cycle as a simple tool for handling all file reading for you. As simple breakdown of the cycle is this:

  • All heading and detail lines (H or D in position 17 of the output specifications) are processed.
  • The next input record is read and the record identifying and control level indicators are set on.
  • Total calculations are processed. They are conditioned by an L1 through L9 or LR indicator, or an L0 entry
  • All total output lines are processed. (identified by a T in position 17 of the output specifications)
  • It is determined if the LR indicator is on. If it is on, the program is ended
  • The fields of the selected input records are moved from the record to a processing area. Field indicators are set on
  • All detail calculations are processed (those not conditioned by control level indicators in positions 7 and 8 of the calculation specifications) on the data from the record read at the beginning of the cycle

Don't spend too much time worrying about the Logic Cycle

Realistically no body uses the logic cycle anymore... not since the mid 90's anyway.... modern RPG /FREE and a modularized clean coding approach is obviously the favored technique. Having said that, there are still thousands of old programs (written using the logic cycle) that are out in the wild quietly working... like they have for decades.

So, be aware of the logic cycle but don't think you have to use it to write new code because some old gray haired hippy programmers tells you it's super efficient.

Sometimes good style and efficient runtime performance don't mix. Wherever you face a conflict between the two, choose good style. Hard-to-read programs are hard to debug, hard to maintain, and hard to get right. Program correctness must always win out over speed.

  • Make it right before you make it faster.
  • Keep it right when you make it faster.
  • Make it clear before you make it faster.
  • Don't sacrifice clarity for small gains in efficiency 

Old RPG in a modern world

I've been writing all my programs in RPG Free format since attending RPGWORLD back in 2007. Even though I was a few years late to the party, the gathering of the IBM i programming elite gave me the impetus to finally commit myself to the new and improved style of RPG coding.

I regularly work for clients that have twenty year old programs running in production, the developers still write in RPG3 and shun any development techniques that have been introduced this century.

"I haven't got time to learn all that newfangled business" is the bad programmers war cry.

"Thats why your code is unreadable, the system runs like a dog, change management is out of control and there are so many bugs" is my silently worded response.

Rpg four

As an RPG programmer I am amazed that other programmers are simply not interested in learning the benefits of the latest generations of RPG. There are lots of flavours and any programmer worth his salt should be proficient in all of them: RPG2, RPG3, RPG400, RPGILE, RPG4, RPG/Freeformat

I wish IBM had simply named it RPG1-2-3-4-5 but in true befuddling IBM style the Report Program Generators language naming structure is nearly as poor as the naming standards for the System i itself.

Anyway, I digress, my task yesterday was to write a bunch of little utility programs adding up various elements of the JBA System21 Accounts Receivable database to compare values with what was stored in the accounts hierarchy.

For example, "Does the total value of orders actually match all the orders added together?" and "How many invoices and how are outstanding this year by month" relatively simple to write and the first program was already written.

Let's look at the first program which is clearly written in RPG from the 1980's or early 90's but, quite amazingly, was written in 2008!

Some programmers are frozen in the land of the 1980's and refuses to move up from RPG3. The old style code works, but can be easily upgraded to a twenty year newer version which is vastly more readable, efficient and easy to maintain - in a few simple steps.

So here's the code:

H 1
 * program to fix SLP06 records
FEFP01L01IF E K DISK KINFSR *PSSR
FACSUMMARUF E K DISK KINFSR *PSSR
ILDA E DSLDA
IPSTAT ESDSPSDS
C *NAMVAR DEFN *LDA LDA
C IN LDA
C K01 KLIST
C KFLD CUSN06
C L#CONO SETLL ACSUMMAR
C L#CONO READE ACSUMMAR 06
C *IN06 DOWEQ *OFF
C K01 CHAINE FP01L01 02
C *IN02 IFEQ *OFF
C Z-ADD AMOUNT BODV06
C ELSE C Z-ADD 0 BODV06
C END
C UPDAT SLR06
C L#CONO READE ACSUMMAR 06
C END
C SETON LR
C OUT LDA
/COPY OSLCOPYSRC/QRPGSRC,PSSR

Notice there are no comments, not even an author name.

I decided to take this program and upgrade it to RPG/FREE so I can show him the thought process between versions and hopefully entice him to write newer code: RPG Free Format is neater, easier to read and more efficient.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>