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:
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.
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:
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 int he 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.
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:
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.