23
Sat, Nov
1 New Articles

Is It Time for Free-Format?

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

There are two primary reasons that I've not been a strong advocate for the free-format RPG IV syntax that was introduced with OS/400 V5R1:

  1. Most people were running OS/400 V4 and therefore would not be able to utilize any of the example code I wrote about.
  2. The syntax was too different from what I was used to.

Let me explain these reasons.

First, until recently, most people were running one of the OS/400 V4 releases. Sure, a large number of shops moved to V5R1 within a year of it first being announced, but of those, a large number also had secondary systems installed and had to be compatible with V4. So V5R1-specific features wouldn't cut it.

Second, I feel that the syntax used in free-format is a bit more of just "make it work" than it is "make it right." I find that even today with V5R5 on the horizon (yes, I said V5R5) I'm still struggling every time I allow myself to write with /free syntax.

Certainly, many others jumped on the /free bandwagon right way. These people were from many different backgrounds and cultures. Some were former C, Visual Basic, or COBOL programmers who are used to free-format syntax. Some were instructors who benefited by being able to fit their examples on their PowerPoint slides when free-format was used. Others were leading-edge iSeries professionals who always jump on the latest features.

Today, some people say, "Most of the shops I see are using /free." I have a pond in my back yard, and most of the ducks I see are mallards, so does that mean most ducks are mallards? This is specious reasoning at its best.

In June 2006, however, most iSeries shops are running OS/400 V5R1 or later. This means most shops have the ability to use free-format RPG IV syntax if they choose to. Therefore, I have made a decision to use free-format RPG IV syntax where appropriate (but not 100%). I will use it where it is best used: in calculations specifications that require longer lists of parameters or in expressions (including built-in functions) that are a bit lengthier than usual.

Here are some guidelines I've come up with for writing /free RPG IV syntax:

  • Use the // comment syntax.
  • Force yourself to press the semicolon (;) key before pressing Enter. That way, you can always remove extra semicolons rather than never see them in the compiler error listing.
  • Always use uppercase/lowercase letters.
  • Always indent conditional logic.
  • Always line up the ENDIF statement with its corresponding IF statement. The same holds true for the other conditional operation codes.
  • Try to avoid mixing free- and fixed-format syntax in the same subprocedure or mainline calcs. While it is OK to have subprocedures in free-format and mainline calcs in fixed-format, try to avoid using both syntaxes in the same area of the program source code.
  • Remember that IBM introduced the EVALR (eval right-justified) opcode back in V4R2. It copies data right-justified to the result. This is a way to get the MOVE opcode into your /free code without burning out your brain cells. The EVAL opcode is similar to the MOVEL opcode.
  • Use %CHAR(myNumVar) to convert numeric data to character.
  • Use %EDITC(myNumVar : 'X') to move numeric to character, unedited with leading zeros.
  • Avoid symbols (such as @, #, and $) in variable or subprocedure names. Nothing's worse to read than code with a bunch of special symbols in field names. What's more, most of these symbols don't convert to other CCSIDs properly. So if you have a field named W#CNT and the source code moves to Italy, the Italian programmers are going to see garbage for the second character of the field name. Do you really want the extra work later on?

Gotcha!

I've also run into several gotchas with free-format. Most are minor, but a couple of these minor issues seem to occur with every RPG IV programmer who embraces the /free syntax.

Here are some of the gotchas I've run into while writing /free RPG IV syntax:

The MOVE and MOVEL opcodes are not supported in /free syntax. There are myriad built-in functions that allow you to accomplish similar function to MOVE and MOVEL, and you'll need to learn those functions.

Be sure you enter a semicolon at the end of every RPG IV statement. If you don't, you'll get an error on the next line in the program that makes little sense to you. This is because the syntax allows for automatic line continuations (which is good), but a byproduct is that the compiler doesn't know where the statement ends until it finds a semicolon.

So the following can give you headaches:

  if  (a = b)
       X = X = 1;
  endif;

What's wrong here? The semicolon is missing from the first line. It should be:

     if  (a = b);

As it was originally written, the compiler thinks it is the following:

  if  (a = b) X = X = 1;

This is illegal syntax in /free RPG IV. It's valid syntax in other languages, but not in RPG IV, so be careful or you might stare at your errors for days. The correct syntax would be:

  if  (a = b);
       X = X = 1;
  endif;

You also need to pay attention to parens around conditional expressions. This technique is permitted on all conditional operation codes except the FOR operation. So the following DOU operation is valid:

   DOU  (i >= %size(custname));
        i = i + 1;
         //  do something here.
   enddo;

But the following FOR operation is not valid:

   for  (i = 1 to %size(custname));
         //  do something here.
   endfor;

The syntax does not support parens around the FOR operation's conditional expression. Although it's technically not an expression, in C, JavaScript, C++, and other languages, it is treated the same, so for (i = 1 to %size(myvar)) is valid in other languages, just not RPG IV. And since parens are supported around other conditional expressions, you could easily deduce that FOR supports them as well, but it doesn't.

Also, the syntax checker in SEU doesn't work with /free syntax until V5R4. The good news is that /free syntax checking is supported in V5R4.

Next, the /end-free directive is required even if you jump back into fixed-format. For some reason a "C" in column 6 doesn't imply that you've ended free-format and want to go back to fixed-format. You get a compiler error instead. I often include a "SET ON LR" statement at the top of my source code, but most people put it at the bottom of the calc specs. To make it stand out, I habitually code it in fixed-format. For example:

      /free
          if miTime <> *BLANKS;
             myDate = MiTimeToDTS(miTime);
          endif;
      /end-free
     C                   eval      *INLR = *ON

If I were to leave off the /end-free line, I'd get a compiler error. Again, this is a gotcha, not a design flaw. You could just as easily (if not more easily) have code like this:

      /free
          if miTime <> *BLANKS;
             myDate = MiTimeToDTS(miTime);
          endif;
          *inlr = *ON;
      /end-free

Next, the /free syntax is evolving with each release. For example, the short-form math did not exist in V5R1; it was introduced in V5R2. In V5R3, IBM changed the way the short-form math works. So you have three releases of /free that support three different syntaxes for math.

V5R1:

    count = count + 1;
    bytes = bytes * %size(custname) + 1;

V5R2:

    count += 1;
    bytes *= %size(custname) + 1;

V5R3:

    count += 1;
    bytes *= (%size(custname) + 1);

What changed is that parens are now implied around the right-side values when short-form math is performed. In addition and subtraction, this doesn't have a big impact, but when multiplication is performed, it has a huge impact. For example:

This

    bytes *= %size(custname) + 1;

means this in V5R2

    bytes = bytes * %size(custname) + 1;

but in V5R3, it means this:

    bytes = bytes * (%size(custname) + 1);

For what it's worth, the new interpretation is the correct one, as the original parsing was clearly producing the wrong result.

If you're on V5R2 or V5R3, you can install and apply the following PTFs to make sure the parser is correctly converting the short-form math operator:

For V5R2M0, use SI17130.

ForV5R3M0, use SI17150.

That's all on free-format for now. Watch for more on this topic in future issues of RPG Developer and on iSeriesTV.com, which premieres on July 18.

Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.

BOB COZZI

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

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: