Practical RPG: Use Prototyping to Maximize Productivity PDF Print E-mail
Programming - RPG
Written by Joe Pluta   
Wednesday, 06 January 2010 00:00

Prototypes are one of the most powerful enhancements in ILE RPG, particularly in free-form; this article shows you just how productive prototypes can be.

 

The CALLP opcode is arguably the most significant enhancement in ILE RPG. With the exception of free-format RPG, prototypes are certainly the most extensive addition to the language. Used properly together, the syntax of defining parameters as an extension of the D-specification and the various keywords that assign attributes to those parameters can completely transform the RPG language in ways you might not have foreseen. This article explains one of the subtlest but most important of those changes.

Replacing the PLIST

The simplest way that programs are changed is the replacement of the PLIST with the prototype. In its simplest guise, the prototype is really a parameter list in the D-specs. For example, take a PLIST that looks like this (some spaces have been compressed to fit the page width):

 

  c   PMISND     plist

  c              parm          PS_MessageID      7

  c              parm          PS_MessageData   80

 

This is very straightforward—two parameters, one a seven-character alpha field containing the message ID and the other an 80-character field containing message data. The corresponding prototype is here:

 

 d PMISND          pr                  extpgm('PMISND')

 d   msgid                        7    

 d   msgdta                      80    

 

Astute readers might recognize this prototype from my previous article on program messages (albeit with a minor difference, which I'll be discussing shortly). The two techniques do pretty much the same thing, although the prototype adds the additional information of the program to call; with the PLIST, this information is provided in the CALL opcode. Let's compare the two approaches:

 

   move      'CUS0001'     PS_MessageID

   movel     CustNum       PS_MessageData

   call      'PMISND'      PMISND

    

   eval      wMessageID = 'CUS0001'

   eval      wMessageData = %editc( CustNum: 'X')

   callp     PMISND( wMessageID: wMessageData)

    

You'll notice some very similar code. And even though I went a little out of my way to write the PLIST version using the RPG III style of code, complete with MOVE and MOVEL, while using RPG IV's EVAL opcode for the prototype technique, you can see the similarities in the two code fragments. Each one has two field initialization statements followed by the call. However, I cheated for the RPG IV fragment by leaving out a couple of lines of code. Can you tell what they are? Here:

 

 d  wMessageID      s             7    

 d  wMessageData    s            80    

 

I have to define the two work variables used to transport the message ID and message data from this program to the PMISND program. Somewhat counter-intuitively, the two parameter definitions in the prototype don't actually define variables; they are simply placeholders to tell you the size and type of the parameters. In order to actually use the prototype, I have to pass in variables that match the size and type of the prototype. Typically, that involves using work variables, as shown in the code.

 

I don't have to create additional variables when I use a PLIST. Since each PARM line in the PLIST definition is its own C-specification, I can use the old RPG III trick of defining the result field right in the specification. I normally frown on that particular technique because it combines two programmatically distinct tasks: variable definition and business logic. But I consider a PLIST to be really a definition statement, not real business logic, so I'm OK with defining the variables right there and saving a few lines of code. Heck, without a D-spec, you'd have to define them in MOVE statements, usually in your *INZSR, anyway. Defining them in the PLIST just makes it a little easier.

 

Anyway, the result here is that you have to actually add a couple of lines in order to use the prototype methodology. Not exactly a productivity booster, eh? But wait, that's not the whole story. With just a couple of keywords, you'll see how quickly the game changes.

Eliminating Work Variables

The biggest timesaver when using prototypes is the ability to remove work variables. Work variables are not just an annoyance, although they certainly are that, especially since they require you to jump back and forth between the business logic and the variable definitions. But more importantly, work variables are often a source of program errors, including errors that aren't clear until run time. Overflows and truncation occur when you forget to resize work variables, because even the most diligent programmer doesn't always use the LIKE keyword when defining variables.

 

With a couple of minor changes to the prototype, work variables disappear or at least can be reduced significantly in number:

 

 d PMISND          pr                  extpgm('PMISND')

 d   msgid                        7    const

 d   msgdta                      80    const

 

The change may not be completely obvious at first, but look at the keyword section (the rightmost column) of the D-specs for the two parameters. Each one now has the CONST keyword, which tells the compiler that this variable cannot be changed by the calling program. This allows the compiler to create temporary work variables to hold the data being passed to the called program or procedure (in addition to calling programs, prototypes can be used to call subprocedures within your programs as well as procedures in service programs). This seems like a very simple change, but the effect on the code is actually quite pronounced:

 

   callp     PMISND( 'CUS0001': %editc( CustNum: 'X'))

 

Note that I don't have to do any initialization. I can pass a literal or even a computed value to the procedure without using a temporary work variable. I also don't have to define the wMessageID and wMessageData fields in the D-specifications. Adding those simple keywords to my prototype reduces my code from five lines to one! That is what I mean when I say prototypes make you more productive.

 

You might note that I did have to include the prototype, so that's another three lines of code, but in a production environment, you would definitely have the prototype in a /COPY module. That then brings up the issue of managing include files, but that's more an issue of site standards rather than programming methodology.

 

I called this section "Eliminating Work Variables," but really it's only part one of that particular exercise. Another aspect of using prototypes specific to procedures is the return value. Return values are definitely a new concept to RPG programming, but once you get the hang of them, you can remove nearly all of your temporary variables and—at the very least—all of your global variables. I'll discuss global and temporary variables and their relationship to the return value in another article. Until then, enjoy your prototypes!

 

 

 


Joe Pluta
About the Author:
Joe Pluta is the founder and chief architect of Pluta Brothers Design, Inc. and has been extending the IBM midrange since the days of the IBM System/3. Joe uses WebSphere extensively, especially as the base for PSC/400, the only product that can move your legacy systems to the Web using simple green-screen commands. He has written several books, including E-Deployment: The Fastest Path to the Web, Eclipse: Step by Step, and WDSC: Step by Step. Joe performs onsite mentoring and speaks at user groups around the country. You can reach him at joepluta@plutabrothers.com.

 

MC Press books written by Joe Pluta available now on the MC Press Bookstore.

 

Developing Web 2.0 Applications with EGL for IBM i Developing Web 2.0 Applications with EGL for IBM i

Joe Pluta introduces you to EGL Rich UI and IBM’s Rational Developer for the IBM i platform.

List Price $39.95
Now On Sale
 
WDSC: Step by Step WDSC: Step by Step
Discover incredibly powerful WDSC with this easy-to-understand yet thorough introduction.

List Price $74.95
Now On Sale
 
Eclipse: Step by Step

Eclipse: Step by Step


Quickly get up to speed and productive using Eclipse.

List Price $59.00

Now On Sale
 
Read More >>
Last Updated on Wednesday, 06 January 2010 00:00
 
OzzieH
Yep. Been there done that. I am currently out of favor with this method fwiw. It looks good and seems a workable solution UNTIL your module has two dozen routines, and you need say, 8 prototypes for one application, and later you have 5 applications that need several of those prototypes. Then it becomes messy. The C world does not do this, and they haven\'t done it since day one, so I have reverted back to the method. It tends to make your compiled objects larger, but the readability is higher. Like other things, ymmv. This is just where I am at with this whole thing right now.
efnkay
I understand. Once you get used to a good idea, like myself, I always start looking for a better way. But these DEFINE\'s answer at a glance what procedures the (MAIN) is calling, and as mentioned, what specifically got compiled into your (MAIN). In fact my "Prototyped API\'s Module" which is compiled into a *SRVPGM, has about 5 or 6 dozen procedures in the module, (Many are different prototypes calling the same external API.) Which is why this approach toward compiling prototypes into your (MAIN), is an easy way to manage object size. And keeps your "where used" information down to relevant and actual use of any procedures. Been there and you may want to go back there. Because...When would you ever NOT consider the size of the program objects you create...??? Maybe when disk space is an inifnite number?
OzzieH
When would I ever NOT consider?...: When the price of DASD falls to $100/terabyte---which is TODAY. You don\'t have to take my word for it, run your own tests. The extra space consumed by unused prototypes is negligible in terms of today\'s storage capacity and prices. Like I said, ymmv.
efnkay
Well lets just disagree. It\'s usually someone else who comes along behind such coders and cleans up all the "litter". Translate: Unnecessary or un-executed code. Disclaimer: Unless the commented out, or un-necessary code is relevant to keeping a history of modifications. Otherwise it is just clutter. An ancient term for it might be a "standard". Which code in our shop must comply with...Yours? Wouldn\'t fly in my shop and wouldn\'t get on our machine old buddy...!!!
efnkay
Let me get out here real quick and say "no offense" intended. My point being not to judge anyone by "our" standards. But suffice it to say that if you don\'t follow some best-practice techniques, or some kind of standard in your shop, or just in your personal coding standards...Then you should consider that you may already be judged. Good luck!
Please login to make comments.
User Rating: / 2
PoorBest 
   MC-STORE.COM