27
Fri, Dec
0 New Articles

How Much Do You Really Know About Modern RPG Programming?

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

ILE RPG and free-format have been around for years. However, not everyone is taking advantage of these and other features of modern RPG. Take the quiz below and find out how much you really know about modern RPG programming.

 

Modern RPG implies modular and structured programming techniques: using modules and service programs, procedures, functions, and so on. However, using procedures and functions generally requires passing parameters between them. That's where things can start to get a bit complicated. Unexpected errors start occurring after a function call, and after hours of fruitless investigation, you finally realize that the function call changed a value it shouldn't have. There are two keywords, VALUE and CONST, to prevent that from happening. That brings us to the first question of this quiz:

 

Which keyword is best to keep your parameters safe: VALUE or CONST?

 

  1. VALUE
  2. CONST
  3. It depends

 

If you replied either a or b without hesitating, then let me tell you that you're wrong. The correct answer is c, "It depends." Let's very briefly go over how each of these keywords works to explain why c is the correct answer:

 

  • VALUE is nice because you're 100 percent sure that the original variable is not changed (unless something really, really weird happened). However, it's not a good idea to use this keyword with big parameters or intensively used procedures because the system will have to copy the parameter's contents to a new variable every single time the procedure is called. That's the way to ensure that the original variable remains untouched.
  • CONST is also nice because there's no content copying, so the procedure gets called immediately, regardless of the size of the parameters; only the pointers are passed. It's faster and also "cleaner" in the sense that you're not allowed to intentionally change the parameter's contents. At compile time, the system checks and flags any attempts to change the content of a parameter defined with const.

 

As you can see, both keywords have pros and cons. You need to carefully define your procedure's parameters, taking into account the parameters' sizes, what you're going to do with them, and how often the procedure will get called, if you know that in advance. I'm sure that some readers won't agree with this answer, so feel free to speak your mind in the "Comments" section below.

 

Some programmers who have moved to free-format RPG complain that the language is being held back by some annoying constraints. Let's see how much you know about free-format RPG, starting with the question below:

 

Is it possible to write the header section of an RPG program (the H-specs) in free-format?

 

  1. Nope, you're still stuck with ugly fixed-format H-specs in your "modern" RPG code.
  2. Yes, you can!

 

The correct answer is b. One of the least-known parts of free-format RPG was introduced with V7.1 TR7: the ability to write control, file, and definition specification lines (H-, F-, and D-specs) in free-format. Let's look at an example of a control specification migration from fixed- to free-format.

 

The following is a group of "traditional" control specifications:

 

H CURSYM('$') DEBUG(*YES) DECEDIT('. ') DATEDIT(*MDY) DATFMT(*MDY/)

H TIMFMT(*ISO) COPYRIGHT('(C) Copyright ACME Company - 2015')

 

This becomes a neat piece of code in free-format:

 

Ctl-opt CURSYM('$')

         DEBUG(*YES)

         DECEDIT('. ');

Ctl-opt DATEDIT(*MDY)

         DATFMT(*MDY/)

         TIMFMT(*ISO);

Clt-opt COPYRIGHT(' (C) Copyright ACME Company - 2015');

 

The indentation might seem like an idiosyncrasy, but it's there for a reason: future maintainability. If I wanted to change the option used in DATFMT, for instance, I'd have to rewrite the fixed-format lines, while the same operation in free-format is much simpler. How cool is that?

 

Another common complaint is related to the operation codes that people can't find in free-format, which brings me to the next question:

 

What are the free-format replacements for TAG, GOTO, and CABXX?

 

  1. The %JumpHere, %JumpTo, and %JumpWhenxx BIFs
  2. There aren't any. Restructure your spaghetti code ASAP.

 

The correct answer is b. These three operation codes don't have replacements, direct or indirect, in free-format—and if you ask me, that's a good thing. Let's do a quick refresher, starting with the last of the three: CABXX, the "Compare and Branch" operation code, has been around for ages; it's one of the workhorses of "old" RPG. If you're not familiar with it, the functionality provided by CABXX is similar to CASXX; the difference is that it doesn't execute a subroutine, like CASXX. Instead, it "jumps" to a label somewhere else in the code.

 

TAG (the way to indicate a label) and GOTO (another way to jump to a label) were dropped for the sake of readability (and getting rid of the dreaded spaghetti code, hallmark of older versions of RPG). This means you'll need to reengineer your code to "convert" the CABXX, TAG, and GOTO operations to free-format. Let's explore the options.

 

Sometimes, a TAG and a GOTO delimit a piece of code that could be a subroutine or even a procedure in better-structured source code. Using the CASXX free-format alternative (an IF statement surrounding an EXSR instruction) might be a solution for these cases. However, there are times when you just need to "jump" to a particular place in the code—leave a cycle or a subroutine, for instance. For these cases, there is ITER (iterate), which jumps to the top of a loop. That can be any type of loop: DOWXX, DOUXX, or FOR. There's also LEAVE, which jumps out of the loop, and LEAVESR, which (you guessed it) leaves the subroutine.

 

For the rest of the GOTO situations, you need to take a good hard look at the code and decide what to do. It's not the best of solutions, but you can decide to leave it as is (for a while, at least), because you can mix fixed- and free-format code, as long as you signal the beginning and end of your free-format code with the appropriate directives. I'm not encouraging you to leave those GOTOs permanently—just until you get some experience with the whole ILE thing.

 

The Open Query File (OPNQRYF) command, along with the GOTO and TAG operations codes, sits at the top of the RPG programmer's worst nightmare list because what started as a good, simple idea became a huge mess in no time. Someone has to maintain that mess … and it's never easy. This begs our next question:

 

Is there a way to get rid of the dreaded OPNQRYF in modern RPG?

 

  1. Not yet, but there's still hope.
  2. Yes, by using an SQL cursor in your RPG program.

 

Again, the correct answer is b. A cursor is a way to use an SQL SELECT statement as if it were a keyed file. In a real-life OPNQRYF, big QRYSLT values are common, leading to nightmarish commands that don't always perform as we'd wish. By dynamically defining data access (or cursor), you can replace the dreaded OPNQRYF with something simpler and much easier to maintain. The solution is defining a flexible cursor using host variables in the DECLARE statement.

 

Let's say I want to define a cursor named mainCursor that reads data from a file named InvMst. I want to be able to indicate the warehouse ID (that's field WHID) dynamically. The cursor's DECLARE statement looks like this:

 

/Free

   Exec SQL

     DECLARE mainCursor CURSOR

     FOR

     SELECT InvMst.ItemID, ItemDesc, ExpDate

     FROM   InvMst InvMst

     INNER JOIN ItmMst ItmMst ON InvMst.ItemID = ItmMst.ItemID

     WHERE WHID = :K_WHID;

 

Notice the line in bold? The WHERE clause with host variables (in this case just one, but you can use as many as you want) replaces the QRYSLT part of OPNQRYF. This variable's value is not known when the cursor is declared, but that's not a problem, because the DECLARE statement is more concerned with the SELECT clause—it needs to know which columns are going to be part of the temporary result table it needs to create. K_WHID matters when the FETCH statement is executed. That's when K_WHID's value is going to be used to replace the host variable name in the statement.

 

Creating a replacement for an OPQRYF is feasible, assuming that you are able to understand what's in the QRYSLT part of OPNQRYF. It can be as easy as "translating" the QRYSLT code to the appropriate SQL comparisons and putting them in the WHERE clause of the DECLARE statement.

 

If you answered any of these questions incorrectly, you might not know as much about modern RPG programming as you thought. My book, Evolve Your RPG Coding: Move from OPM to ILE ... and Beyond, explains these and many other topics in depth, with examples. It covers the whole OPM-to-ILE journey:

 

  • How to go from monolithic programs to flexible modules, programs, and service programs
  • How to write structured, well-documented, and maintainable code
  • How to smoothly migrate from fixed- to free-format RPG

 

Additionally, the book provides one of the most comprehensive chapters about SQL ever written for an RPG book. It covers DML and DDL, as well as embedded SQL in RPG programs, and its opposite: the creation of stored procedures and user-defined functions from RPG code. Then it goes beyond ILE, discussing application modernization, with special emphasis on database and user interface modernization. These are broad topics for which the book provides guidelines, tools, and strategies to help you pursue a successful modernization initiative.

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: