• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Assertions, Take 2

    February 12, 2024 Ted Holt

    It’s been almost 20 years since Cletus the Codeslinger introduced assertions to the IBM midrange world, and in that time, I have included many assertions in my RPG programs. During that 20 years, RPG has changed a bit and Cletus has quit writing articles (but not source code), so I’ve taken it upon myself to update the code he gave us.

    First, however, it might be a good idea to briefly review the topic of assertions for readers who don’t know what they are. An assertion is a program statement or command that cancels a program if a fatal condition is found. It provides a way for a programmer to tell the computer that a certain condition ABSOLUTELY, POSITIVELY MUST BE TRUE, OR ELSE!

    Here’s an example:

    assert (Price = *zero: 'Assertion failed: price is not zero');
    

    This says that if the PRICE variable does not have a zero value at this point, something has gone terribly wrong and disastrous consequences will certainly follow. Therefore the program must be canceled. Assertions always cancel the program, so programmers don’t use them for data validation.

    This story contains code, which you can download here.

    That’s it in a nutshell. I encourage you to read Cletus’s article for more information before continuing.

    RPG has changed in many ways since Cletus shared his routine with us. First, RPG went free-form. I’ve gotten so used to free-form RPG that working on fixed-format RPG source code is about as comfortable as wearing my gum boots on the wrong feet.

    Second, IBM added the SND-MSG operation code, which can send escape messages. Underneath the covers, everything’s the same. SND-MSG calls the QMHSNDPM API to send message CPF9898.

    Here’s the updated code, both the prototype and the subprocedure itself.

    dcl-pr Assert;
       Condition   ind           value;
       Message     varchar(80)   value   options(*nopass: *trim);
    end-pr Assert;
    
    dcl-proc Assert;
       dcl-pi *n;
          Condition   ind           value;
          Message     varchar(80)   value   options(*nopass: *trim);
       end-pi;
    
       if not Condition;
          snd-msg *escape Message %target(*caller);
       endif;
    
       return;
    
    end-proc Assert;
    

    Notice the message destination: %TARGET(*CALLER). So far this has worked properly for me, no matter how far down in the call stack the SND-MSG is. I have also sent the message to the program boundary by referencing the first subfield of the Program Status Data Structure, and I can’t find any difference in behavior between the two methods.

    dcl-ds  psds  psds;
       PgmName   char(10);
    end-ds;
          
    if not Condition;
       snd-msg *escape Message %target(PgmName);
    endif;
    

    The new assert routine is so short that you can copy and paste it from this page, but I put it in the downloadable code in case you prefer it that way.

    In fact, the new assert routine is so short that I wonder if we really need an assert subprocedure anymore. Contrast the following statements:

    (A)
    assert (Price = *zero: 'Assertion failed: price is not zero');
    
    (B)
    if (Price <> *zero);
       snd-msg *escape 'Assertion failed, price is not zero' %target(*caller);
    endif;
    

    Does (A) have any advantage over (B)? The only one I can think of it that the word assert sticks out, making the programmer’s intent obvious.

    But if the message includes the word assertion, as this example does, why bother with a subprocedure call?

    And getting rid of the subprocedure call has to be better for performance.

    As I see it, the important thing is not how I write assertions, but that I do write assertions. I like the peace of mind I get from knowing that the data have proper values, and I like my programs to tell me where they canceled and why. It beats spending hours with a debugger trying to recreate a catastrophe.

    RELATED STORY

    Programming with Assertions

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, assertion, 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

    IBM Patches New Security Vulns In IBM i Components, Power Firmware Thoroughly Modern: From Tradition To Transformation For IBM i In The Era Of Cloud And AI

    3 thoughts on “Guru: Assertions, Take 2”

    • Leslie Turner says:
      February 13, 2024 at 6:43 am

      Thanks Ted! Your tips are always helpful!

      Reply
      • Timothy Prickett Morgan says:
        February 13, 2024 at 8:14 am

        He is, in fact, the best.

        Reply
    • Allister Jenks says:
      May 28, 2024 at 10:42 pm

      Someone once said “Don’t look for performance gains until you have performance problems.” If I were writing a real-time or extreme volume program, I might opt for skipping the procedure.

      However, 99.99% of the time this won’t be true and for the sake of hiding a dozen-or-so line procedure at the bottom of my program, I’d opt for that approach every time for two reasons.

      First, it’s one line versus three. Succinctness greatly improves the readability of code. It takes some skill to write code that works, but much more skill to write code people can understand (including our future selves).

      Second… I will never be comfortable with having a semicolon on the if statement line. I suspect RPG is the only language on the planet to do this and it always looks wrong to me. Any opportunity to remove an if is an opportunity to save my sanity.

      In the same vein as the simple assert procedure, I also use a simple “cmd( )” procedure that takes away the QCMDEXC name as well has the need to specify length. Now when I need to run a CL command it’s just “cmd(‘DLTF QTEMP/WKFILE’);” and the like. Again… succinctness, and therefore readability, wins.

      Reply

    Leave a Reply Cancel reply

TFH Volume: 34 Issue: 08

This Issue Sponsored By

  • New Generation Software
  • Fresche Solutions
  • Kisco Systems
  • Raz-Lee Security
  • WorksRight Software

Table of Contents

  • The State Of The Power Systems Base 2024: The Operating Systems
  • Thoroughly Modern: From Tradition To Transformation For IBM i In The Era Of Cloud And AI
  • Guru: Assertions, Take 2
  • IBM Patches New Security Vulns In IBM i Components, Power Firmware
  • IBM i PTF Guide, Volume 26, Number 6

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