• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: What Is Constant Folding And Why Should I Care About It?

    October 18, 2021 Ted Holt

    Constant folding is a compiler-optimization technique, whereby the compiler replaces calculations that involve constants with the result values when possible. Constant folding is common in modern compilers, and according to the RPG reference, the RPG compiler uses this technique. (See the documentation of the %div and %rem functions, for example.)

    But you and I don’t write compilers. We write business applications. Why then should we care about constant folding? That’s a question worth pondering.

    Consider how I used to have to write RPG in the Dark Ages.

    C                     MOVE CUSTNR    CUSTSV  50
    

    Here I’m copying the customer account number to a save field, perhaps to check for a control break. What could go wrong with this code? Simply this: If someone increases the size of the customer account number field, CUSTSV will no longer be set to the proper value. What’s worse, the error won’t surface until a customer is assigned account number 100000 or higher, which may be a long time after the program change.

    So the wonderful people at IBM decided to let us do this instead:

    C           *LIKE     DEFN CUSTNR    CUSTSV
    C                     MOVE CUSTNR    CUSTSV
    

    One potential source of error is eliminated. I was ecstatic when IBM added the *LIKE DEFN op code to the RPG compiler I was working with at the time.

    Nowadays we do the same thing with definition specifications.

    dcl-s  Cust_Account_Save      like(Cust_Account);
    

    Or at least we should. I have seen a lot of code where the programmer could have used the LIKE keyword and didn’t, and I’m not talking about old code.

    The LIKE keyword has a second argument that increases or decreases the defined size of a variable. For example:

    dcl-s  Limit           packed (7: 2);  
    dcl-s  NewLimit        like(Limit: +2);
    

    NewLimit is defined to be nine digits long, two of which are decimal positions.

    IBM also gives us three built-in functions that retrieve information about data definitions: %SIZE, %LEN, and %DECPOS. These wonderful functions open up even more possibilities for the compiler to use constant folding.  Like the LIKE keyword, they are underused and unappreciated.

    Suppose I need to retrieve a decimal number from a character string. The %DEC function carries out such a task easily, but it has to know how to define the resulting value. I could do this:

    dcl-s  Limit           packed (7: 2);
    
    Limit = %dec (TextValue: 7: 2);
    

    But this is better:

    dcl-s  Limit           packed (7: 2);
    
    dcl-c  Length          const( %Len    (Limit) );
    dcl-c  DecPos          const( %DecPos (Limit) );
    
    Limit = %dec (TextValue: Length: DecPos);
    

    If the definition of LIMIT ever changes, this code takes the change in stride.

    It is a quirk, to my way of thinking, that we cannot place the %LEN and %DECPOS function calls directly into the %DEC function, but such is the case. Instead, we have to use the roundabout method of declaring two named constants. I found this out the hard way, even though this behavior is documented in the RPG reference. Evidently I have more important things to do than read manuals.

    These functions also come in handy when changing the data type. For example, suppose I want a varchar variable that holds the same amount of data as a fixed-length character field. I can’t use the LIKE keyword, but I can do this:

    dcl-s  Address      char(20);
    dcl-s  AddrMod      varchar(%size(Address));
    

    Some compilers are more generous than the RPG compiler when it comes to the use of constants. Suppose I need a variable to be twice as long as a database field. Maybe I’m going to store the hexadecimal representation of the field in the variable. Maybe I’m going to replace each apostrophe with two apostrophes. Some compilers would allow something like this:

    dcl-s  Address      char(20);
    dcl-s  AddrMod      char(%len(Address)* 2);
    

    Not so the RPG compiler. Instead, you have to find another way, like this method, which I consider a kludge.

    dcl-s  Address      char(20);
    dcl-ds AddrMod;
       *n  like(Address);
       *n  like(Address);
    end-ds AddrMod;
    

    The more we can use the LIKE keyword and the data definition built-in functions, the more chances the compiler has to use constant folding. But why should we care about constant folding? Because we need optimized object code? That’s a good reason, but here’s an even better one: Because constant folding makes programs less likely to break when we modify them. You and I have more important things to do than to search for bugs.

    RELATED STORIES

    %DIV (Return Integer Portion of Quotient)

    %REM (Return Integer Remainder)

    %DEC (Convert to Packed Decimal Format)

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, FHG, Four Hundred Guru, IBM i, RPG

    Sponsored by
    Manta Technologies

    The Leader in IBM i Education!
    Need training on anything i?
    Manta is all you need.

    130 courses and competency exams on:
    · IBM i operations
    · System Management and Security
    · IBM i Programming Tools
    · Programming in RPG, COBOL, CL, Java
    · Web Development

    SQL, DB2, QueryProduct features:
    · Runs in every popular browser
    · Available 24/7/365
    · Free Student Reference Guides
    · Free Student Administration
    · Concurrent User License
    · Built-In IBM i Simulator

    You can download our 200-page catalog and take sample sessions at MantaTech.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    As I See It: The Management Challenge The Ease Of API Programming Has To Be Balanced By Heightened API Security

    Leave a Reply Cancel reply

TFH Volume: 31 Issue: 68

This Issue Sponsored By

  • UCG Technologies
  • Profound Logic
  • Computer Keyes
  • Eradani
  • New Generation Software

Table of Contents

  • Planning A Modernization Project? Read This First
  • The Ease Of API Programming Has To Be Balanced By Heightened API Security
  • Guru: What Is Constant Folding And Why Should I Care About It?
  • As I See It: The Management Challenge
  • We Have The Whole World Of Cloud In Our Hands

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • With Power11, Power Systems “Go To Eleven”
  • With Subscription Price, IBM i P20 And P30 Tiers Get Bigger Bundles
  • Izzi Buys CNX, Eyes Valence Port To System Z
  • IBM i Shops “Attacking” Security Concerns, Study Shows
  • IBM i PTF Guide, Volume 27, Number 26
  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle