• 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
    FalconStor

    Simplify Secure Offsite Data Protection for IBM Power with FalconStor Habanero™

    IBM i teams are under growing pressure to ensure data is protected, recoverable, and compliant—without adding complexity or disrupting stable environments.

    FalconStor Habanero™ provides secure, fully managed offsite data protection purpose-built for IBM Power. It integrates directly with existing IBM i backup tools and processes, enabling reliable offsite copies without new infrastructure, workflow changes, or added operational overhead.

    By delivering and managing the service end-to-end, FalconStor helps organizations strengthen cyber resilience, improve disaster recovery readiness, and meet compliance requirements with confidence. Offsite copies are securely maintained and available when needed, supporting recovery, audits, and business continuity.

    FalconStor Habanero offers a straightforward way to modernize offsite data protection for IBM i: focused on simplicity, reliability, and resilience.

    Learn More

    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

  • IBM Starts Winding Down Power10 System Sales
  • Guru: Service Programs And Activation Groups – Design Decisions That Matter
  • Strategic Topics To Think About For 2026, Part 1
  • Shield Gooses Performance Of Nagios Monitoring Tool, Adds AI Reporting
  • IBM i PTF Guide, Volume 28, Number 6
  • Rolling The Die In 2026: IBM i Predictions, Take Two
  • Perhaps 2026 Is The Year For Power Systems To Boom A Little
  • Guru: Binder Source Is Your Service Program’s Owner’s Manual
  • Skills Displaces Cybersecurity As Top Concern For IBM i Shops
  • IBM i PTF Guide, Volume 28, Number 5

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