惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
有赞技术团队
有赞技术团队
B
Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
Martin Fowler
Martin Fowler
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
博客园 - Franky
小众软件
小众软件

Comments for IT Jungle

Guru: Where’s The Table? - IT Jungle Will Power Chips Get A Converged Arm Instruction Set Like Z Mainframe CPUs? - IT Jungle Oracle Dips A Toe Into IBM’s EBCDIC World - IT Jungle Guru: Putting Failure Handling In Its Place - IT Jungle Inside The Security Enhancements In ACS - IT Jungle Guru: Claude’s SQL Tip - IT Jungle Guru: Beyond Three-Part Naming – Running SQL Across Remote IBM i Systems - IT Jungle Does AI Mark The End Of The ERP Era? - IT Jungle Guru: Deterministic Application Development With AI - IT Jungle What IBM’s Got Cooking In Db2 For i In The Summer TRs - IT Jungle Guru: Creating Excel Spreadsheets With Python - IT Jungle The Power11 IBM i P05 Entry Machine Finally Arrives - IT Jungle Guru: Analyzing User Session Statistics, Part 1 - IT Jungle Your IBM i Jobs Don’t Live On An Island Anymore - IT Jungle Present Timestamps in the Local Time Zone - IT Jungle Set Your Library List From A Job Description - IT Jungle GenAI Is The Death Of Deterministic Project Budgeting - IT Jungle Guru: Single Threading A Program Execution - IT Jungle Guru: Where’s The Table? - IT Jungle Big Blue Unveils Bob Premium Pack For IBM i - IT Jungle Guru: SQL Sequences In RPG Let Db2 Handle The Counting - IT Jungle DB2 for i 7.2 Features and Fun, Part 1 - IT Jungle Guru: DateTime Rules Of Thumb - IT Jungle Spring IBM i Tech Refreshes Will Come A Bit Later This Year - IT Jungle As I See It: The Surgical Years - IT Jungle After A Few Short Years, VS Code Passes Rational Developer for i - IT Jungle Guru: Managing The Lifecycle Of Your Service Programs – Updates Without Chaos - IT Jungle Where We Are And Where We Are Headed With AI On IBM i - IT Jungle And Then There Were Two: Big Blue Withdraws IBM i 7.4 - IT Jungle Guru: When Attention Turns To You – Writing Your Own ATTN Program - IT Jungle
Guru: Assertions, Take 2 - IT Jungle
Ted Holt · 2026-08-13 · via Comments for IT Jungle
  • February 12, 2024

    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

    Tags: Tags: , , , ,