• 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
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.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

  • Public Preview For Watson Code Assistant for i Available Soon
  • COMMON Youth Movement Continues at POWERUp 2025
  • IBM Preserves Memory Investments Across Power10 And Power11
  • Eradani Uses AI For New EDI And API Service
  • Picking Apart IBM’s $150 Billion In US Manufacturing And R&D
  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20

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