• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Do It Now!

    August 13, 2014 Hey, Ted

    In our RPG programs we protect input operations (CHAIN, READ, etc.) with an error-handling routine that notifies users of record locks. That is, the RecLock subroutine sends a break message to the locking user and displays a window with lock information to the requesting user. This logic is not working in one of our programs, and we can’t determine why. Can you help?

    –Kent

    I’m glad Kent wrote with this problem. It gives me an opportunity to write about a topic that has been on my mind for some time.

    But first, here’s a trivia question for you to think about. What is the best way to debug a program? (I’ll tell you the answer at the end of this article.)

    Back to Kent’s problem. His RPG source code looked something like this:

    C                   Unlock    SomeFile
    C                   DoU       Not %Error
    C     SomeKey       Chain(E)  SomeFile
    C                   If        %Error
    C                   Exsr      RecLock
    C                   EndIf
    C                   EndDo
    
     * . . . more stuff . . .
    
    C                   Update(E) SomeRec
    

    The update was failing due to a record lock. Yet there should not have been a record lock, because the CHAIN should have looped until the lock was gone.

    The problem turned out to be in the RecLock subroutine. Something caused %ERROR to return false, which stopped the do-until loop even though the record was locked.

    There is a valuable lesson here. I learned it in the mid-1980s, when I installed ASNA’s RPG III compiler on a System/34. (Remember those?)

    IBM shipped RPG II with the S/34. ASNA sold some enhancements that added such features as externally-described files and structured op codes (IFxx, DOWxx, etc.). In the box with the 8-inch diskettes (remember those?) was a book, and in that book was the lesson.

    Whoever wrote that book was wise. He (she?) recommended that the programmer either use an indicator immediately when it is set or save the indicator setting in an appropriate variable for later use. For example, not this:

     * ... omitted code
     *                                              HiLoEq
    C           CTYPE     COMP 'E'                      25
    C  N25      CTYPE     COMP 'F'                      25
    C  N25      CTYPE     COMP 'R'                      25
     * ... more code                                      
    C   25                EXSR EXPDT                      
     * ... more code                                      
    C                     EXCPTDTLLIN                     
     * ... more code                                      
    OREPORT  E  1     01      DTLLIN                      
     * ... more code                                      
    O                 25               122 'EXPEDITE'     
    

    But this:

     * ... omitted code
     *                                              HiLoEq
    C           CTYPE     COMP 'E'                      25
    C  N25      CTYPE     COMP 'F'                      25
    C  N25      CTYPE     COMP 'R'                      25
    C                     MOVE '0'       EXPFLG  1        
    C   25                MOVE '1'       EXPFLG       
     * ... more code                                      
    C           EXPFLG    IFEQ '1'                        
    C                     EXSR EXPDT                      
    C                     END                             
     * ... more code                                      
    C           EXPFLG    IFEQ '1'                        
    C                     MOVE 'EXPEDITE'MSG     8        
    C                     ELSE                            
    C                     MOVE '        'MSG              
    C                     END                             
    C                     EXCPTDTLLIN                     
     * ... more code                                      
    OREPORT  E  1     01      DTLLIN                      
     * ... more code                                      
    O                         MSG      122                
    

    In the first code example, the programmer depends on indicator 25 to hold its setting so that it can be reliably used later in the calculation specs and also in the Output specs. This sort of thing works reliably most of the time, but occasionally something happens, often a program modification that unintentionally changes the setting of the indicator, and suddenly a program that has been working reliably goes haywire.

    In the second code example, reliance on the indicator has been removed. Indicator 25 is set once, and its setting is saved in the EXPFLG variable. Even if something else changes indicator 25, this program logic continues to work correctly.

    After I learned this technique, I typically used only one general-purpose indicator (other than screen-handling indicators) per program.

    “That’s all well and good,” I hear you say, “but I haven’t used an indicator in years.” To which I reply, “Wonderful! I’m glad you haven’t.” But Kent’s code shows that we can still make the same mistake in a more modern way. Built-in functions like %ERROR, %FOUND and %EOF are subject to the same error, even when a file name is attached as an argument.

    I suggested that Kent revise his code along these lines.

    D SomeFileError   s               n
    
    C                   DoU       not SomeFileError
    C     SomeKey       Chain(E)  SomeFile
    C                   Eval      SomeFileError = %Error
    C                   If        SomeFileError
    C                   Exsr      RecLock
    C                   EndIf
    C                   EndDo
    

    Reliance on the %ERROR function is gone.

    Now, to answer that question. The best way to debug a program is to not put the bugs into the program in the first place. Best practices, even little ones like the lesson I learned from ASNA almost 30 years ago, reduce the introduction of bugs and promote reliable programming.

    –Ted



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    New Generation Software

    FREE Webinar:

    Creating Great Data for Enterprise AI

    Enterprise AI relies on many data sources and types, but every AI project needs a data quality, governance, and security plan.

    Wherever and however you want to analyze your data, adopting modern ETL and BI software like NGS-IQ is a great way to support your effort.

    Webinar: June 26, 2025

    RSVP today.

    www.ngsi.com – 800-824-1220

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Linoma Software:  FREE Webinar: Managed File Transfer 101.
    System i Developer:  Upgrade your skills at the RPG & DB2 Summit in Minneapolis, Sept 30 - Oct 2.
    COMMON:  Join us at the COMMON 2014 Fall Conference & Expo in Indianapolis, Oct 27-29

    More IT Jungle Resources:

    System i PTF Guide: Weekly PTF Updates
    IBM i Events Calendar: National Conferences, Local Events, and Webinars
    Breaking News: News Hot Off The Press
    TPM @ EnterpriseTech: High Performance Computing Industry News From ITJ EIC Timothy Prickett Morgan

    IBM Cooks Up Online Consultant Shopping Power8 Packs More Punch Than Expected

    Leave a Reply Cancel reply

Volume 14, Number 18 -- August 13, 2014
THIS ISSUE SPONSORED BY:

SEQUEL Software
WorksRight Software
QUERY FILE for IBM i

Table of Contents

  • The Geezer’s Guide to Free-Form RPG, Part 5: File Definitions
  • Do It Now!
  • Admin Alert: More On Porting User Profiles Between IBM i Partitions

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