02
Sat, Nov
2 New Articles

Practical RPG: Use Prototyping to Maximize Productivity

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

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

Joe Pluta is the founder and chief architect of Pluta Brothers Design, Inc. He 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 Developing Web 2.0 Applications with EGL for IBM i, 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 This email address is being protected from spambots. You need JavaScript enabled to view it..


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 productivity using Eclipse.
List Price $59.00

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: