Using Conditional Compiler Directives

Authors' note: In this series of articles we intend to feature some of the more useful and lesser known gems in the RPG IV language. If you have any suggestions for future editions, please let us know.

Imagine you're trying to debug a particularly stubborn problem with a batch program. In desperation, you decide to add some additional logic to record the details of certain variables as the program progresses. Later, having decided you've fixed the problem, you remove these lines from the program. However, you didn't completely fix it. Oops (that is the polite response); now you must put the lines back in again. When you've "fixed" it a second time, instead of removing the lines, you get smart and simply comment them out. Great idea, save for one tiny problem: When you reinstate them again the next time around, you accidentally "uncomment" a line of code that was first commented out during a maintenance fix back in 1987. The program still doesn't work, but at least its for a different reason.

If this scenario sounds familiar, you should know about Conditional Compiler Directives. Though these useful little devils were added to RPG IV in V3R7, many programmers have never heard of them.

Let's look at a simple example:

 * Normal program logic goes here

 /If Defined(TESTING)
 * Include here any lines that relate only to test/debug
C                    Eval       DebugRec = CustomerDS
C                    Write      DebugFile

 * More regular program logic

If this program is simply compiled with the normal CRTBNDRPG directives, the lines between "/If Defined(TESTING)" and "/EndIF" will be excluded from the compilation. That is because the ConditionName TESTING hasn't been set. Think of a ConditionName as a named indicator, and you won't go far wrong. Conditions can be set within the source, or on the CRTBNDRPG or CRTRPGMOD commands. With our simple example, whenever we want to produce a test version, we simply add the DEFINE(TESTING) option to the compile command. This causes the compiler to include all of the previously excluded lines. It can't get much simpler than that. We can also say that a section of source is to be included if the ConditionName isn't defined -- e.g., /IF NOT DEFINED(TESTING).

In addition to being set on the compile command, Conditions can also be set within the source program itself. They can be set with the /DEFINE(ConditionName) directive and cleared via /UNDEFINE(ConditionName).

Two other directives are currently supported:

/ELSEIF {NOT} DEFINED(ConditionName)

The latter is a handy feature that tells the compiler to skip directly to the end of the current source file (i.e., Do not pass Go, do not collect $200...). The links in the following paragraphs will take you to examples of its usage.


Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.

comments powered by Disqus



2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

The Microphone is Open

Add your voice: Should IBM i include open-source RPG tools?

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters