• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Enable or Disable Code at Run Time

    April 5, 2016 Ted Holt

    If you program in RPG or C, you should know how to use compiler directives to enable or disable source code at compile time. IBM i programmers should also know how to enable or disable executable code at run time, and why they would want to do such a thing.

    Let’s talk about the why first.

    One good reason to enable or disable code is for trouble-shooting. You might find it valuable to include commands to:

    • Display the job log
    • Dump program variables
    • Copy QTEMP files to another library
    • Build spooled files (like of the type I showed how to create in the March 1, 2016 edition of Four Hundred Guru)

    Such code can help with elusive problems and need only be enabled until a problem is resolved.

    Another reason to enable or disable code is to give an end user a way to control execution. For example, he may only want certain programs to run during the first and last months of the year. If you make a program check a setting at run time, and give the user a way to control the setting, you relieve the user of the need to make a run time selection. If the program runs from a job scheduler, then such code is the only way for the user to control run-time behavior.

    Now, the how.

    One easy way to enable or disable code is by checking for the existence of an object. Any type of object can serve this purpose. I prefer data structures because they’re easy to deal with in both CL and RPG.

    In the following example, logical variable &DbgIsOn (debug is on) is controlled by the presence or absence of a data structure named DBG.

    dcl  &DbgDta     *char     96
    dcl  &DbgDtaSiz  *uint      2 
    dcl  &DbgIsOn    *lgl              value('1') 
    
    chgvar     &DbgDtaSiz      value(%size(&DbgDta)) 
    rtvdtaara  dtaara(DBG (1 &DbgDtaSiz))    rtnvar(&DbgDta)
    monmsg     cpf1015   exec(do)   /* not found */ 
            chgvar  &DbgIsOn   '0' 
    enddo 
    
    . . . more stuff . . .
    
    if (&DbgIsOn) do
       dmpclpgm
       dspjoblog  output(*print)
    enddo
    

    If the data structure exists in any library that is in the library list, the program sets &DbgIsOn to true, causing the program to produce a dump and a job log.

    The only problem with this method is that other programs may also have tests for the same data structure. This may or may not cause problems. At best, it could slow down processing and produce a bunch of unwanted spooled files. For this reason, I like to add another test, which looks at the value within the data structure.

    Consider a program, which I’ll call AR100.

    dcl  &DbgDta     *char     96
    dcl  &DbgDtaSiz  *uint      2
    dcl  &DbgIsOn    *lgl              value('1')
    
    chgvar     &DbgDtaSiz      value(%size(&DbgDta))
    rtvdtaara  dtaara(DBG (1 &DbgDtaSiz))    rtnvar(&DbgDta)
    monmsg     cpf1015   exec(do)   /* not found */
            chgvar  &DbgIsOn   '0'
    enddo
    
    if (&DbgIsOn) do
       chgvar  &DbgIsOn   value(%scan(AR100 &DbgDta) *gt 0)
    enddo
    
    . . . more stuff . . .
    
    if (&DbgIsOn) do
       dmpclpgm
       dspjoblog  output(*print)
    enddo
    

    This program tests for the existence of a data structure named DBG, just as the previous one did. If the data structure exists, the program also looks for the value “AR100” in the data structure.

    I have seen programmers add code to aid with debugging and/or problem determination, then delete that code once a problem is found. That isn’t wrong, but many times I have found it valuable to retain such code and activate it as needed.

    RELATED STORY

    Easy Printing From CL

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Raz-Lee Security

    Protect Your IBM i and/or AIX Servers with a Free Virus Scan

    Cyber threats are a reality for every platform, including IBM i and AIX servers. No system is immune, and the best defense is prompt detection and removal of viruses to prevent costly damage. Regulatory standards across industries mandate antivirus protection – ensure your systems are compliant and secure.

    Get My Free Virus Scan

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    COMMON:  2016 Annual Meeting & Expo, May 15 - 18, in New Orleans! Great Power Systems event!
    iTech Solutions:  Get your copy of Pete Massiello's The IBM i State of the Union report now!
    Rocket Software:  Extend green screen apps to HTML5 for use in any web or mobile browser.

    IBM Keeps Low Profile While Reaction To ‘Resource Action’ Boils IBM i 7.3 Arrives April 15; 7.2 TR4 Follows in May

    Leave a Reply Cancel reply

Volume 16, Number 08 -- April 5, 2016
THIS ISSUE SPONSORED BY:

Bug Busters Software Engineering
WorksRight Software
UCG Technologies

Table of Contents

  • Surge Of Services In DB2 For i, Part 1
  • Enable or Disable Code at Run Time
  • Refacing Your Database, Part 1

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19
  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18

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