RPG IV STYLE


The overall programming style used in any programming is a matter of personal taste. Normally, a programming environment has established procedures and standards to be followed. The use of the RPG IV language should influence the standards and programming practices of the RPG programmer. The following recommendations are conventions that can be considered and implemented with good RPG IV-style programming practices.

  • Use structured operation codes whenever possible.

  • Avoid the use of indicators, except when communicating with external files that require indicator conditioning.

  • Use top-down logic. Implement a mainline section in the RPG calculations and call important tasks.

  • Write tasks as subroutines or subprocedures and take advantage of independent source files.

  • Where appropriate, use traditional RPG math operation codes (for example, when adding a single value to an accumulator).

  • Use subprocedures in place of complex subroutines.

  • Where appropriate, convert existing RPG code to modern RPG programming practices.

  • Keep routines (i.e., subroutines, subprocedures, subprograms) to a single task.

  • Build hierarchical subprocedures to perform complex tasks.

Each of the preceding recommendations is described in more detail as follows:

Use Structured Operation Codes. Whenever possible, take advantage of the natural expression support provided by the IF, DOW, DOU, and WHEN operations. Most expressions and conditional logic can be expressed much more simply using these operation codes.

Avoid the Use of Indicators. Except when communicating with an external device file that requires indicator conditioning, avoid using indicators. It is difficult to avoid using indicators when communicating with an interactive workstation (WORKSTN) device file. This is due to the nature of that device's definition language (which is indicator-controlled). It also is difficult to avoid the use of indicators when processing any type of external file (such as a database file). Today, signaling—when the end-of-file is reached or when an error occurs—is done through the use of built-in functions such as %EOF and %FOUND.

Use Top-Down Logic. Implement a mainline section in the RPG calculations and call most important tasks. Design and write RPG applications that have the mainline calculations of the main program control the logic of the application. Rather than inserting them within the mainline code itself, write all tasks that are called as subprocedures or subroutines.

Write Tasks as Subroutines or Subprocedures. Take advantage of independent source files. Writing most tasks as subprocedures or subroutines allows the program logic to be specified in the mainline calculations. All work is performed inside the subprocedures or subroutines. This can lead to easier maintenance, and the reuse of program code is greatly simplified.

Use Traditional RPG Math Operation Codes. Where appropriate, such as when adding a single value to an accumulator, use the ADD operation and other traditional RPG math operation codes. The traditional RPG operations for math includes ADD, SUB, MULT, and DIV. These operation codes are still important in light of the EVAL operation. For example, it is often easier, and frequently more accurate, to write a simple expression using the ADD operation. The two lines of code shown in Figure 7.21 perform the same task.

start figure

 .....CSRn01..............OpCode(ex)Extended-factor2+++++++++++++++++++++++++++++ .....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq      C                   Add       1             Count      C                   Eval      Count = Count +1 

end figure

Figure 7.21: Equivalent mathematical operations.

When considering the use of the MULT or DIV operations, accuracy can become an issue. The EVAL operation tends to use a intermediate result field for mathematical operations. This result field is normally a floating-point numeric field. This can lead to results that are different from traditional MULT and DIV operations, particularly where the half-adjust operation extender is used. Figure 7.22 illustrates the MULT, DIV, and EVAL operations with the rounding (i.e., "half adjust") operation extender in use.

start figure

 .....DName+++++++++++EUDS.......Length+TDc.Functions++++++++++++++++++++++++++++      D Count         S                5p 0 INZ(15)      D Sum           S                7p 2 INZ(32.78)      D Average       S                7P 2 .....CSRn01..............OpCode(ex)Extended-factor2++++++++++++++++++++++++++++ .....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq...      C     Sum           Div(H)    Count         Average      C                   Mult      100           Average      **  AVERAGE = 219.00      C                   Eval(H)   Average = Sum / Count * 100      **  AVERAGE = 218.53 

end figure

Figure 7.22: Mathematical operations with rounding.

Use Subprocedures in Place of Complex Subroutines. The use of subprocedures opens up an entirely new dimension to RPG programming. Local variables (i.e., fields that are "visible" only to the subprocedure), functions, and parameters are just a few advantages to using subprocedures. Create subprocedures where subroutines of any significant length or reusability would normally be used. This allows parameters to be passed to the subprocedure while allowing the subprocedure to retain access to the fields and files declared in the so-called mainline calculations.

Where Appropriate, Convert Existing RPG Code to Modern RPG Programming Practices. The use of indicators, COMP operations, and even nested IFxx operations can be difficult to read and maintain. As programs are modified, attempt to correctly modify this existing code to conform to modern RPG constructs. Convert nested IFxx-ANDxx-ORxx-ENDIF statements to SELECT-WHEN-OTHERWISE constructs. Convert COMP statements and indicator conditioning to IF statements with natural expressions.

Keep Routines (e.g., Subroutines, Subprocedures) to a Single Task. Avoid lengthy subroutines or subprocedures. Attempt to design and write subroutines and subprocedures that perform one and only one main task, and then return. Lengthy subroutines can be burdensome, inflexible, and difficult to enhance. When a task comprises several smaller tasks, create a subprocedure for each smaller task. Then create a functional subprocedure that calls each smaller task in the proper controlling sequence. See the following programming convention for more information.

Build Hierarchical Subprocedures to Perform Complex Tasks. A high level of modularization can be more beneficial than creating larger, more monolithic subprocedures. Each part of a program, (i.e., a task) can be placed into its own stand-alone subprocedure or subroutine. Subprocedures should be used for algorithms and routines that stand on their own. Subroutines, which can be called from mainline calculations or subprocedures, should be used to reduce redundant coding within a procedure.

The boundaries of definition between a subprocedure and subroutine are not thick. Subroutines have been with the RPG language since its inception. Subprocedures are new to the RPG language. Subprocedures allow additional support for parameters, local variables (scope), importing and exporting data, and full modularity. Subroutines, on the other hand, are evoked (i.e., loaded) faster than subprocedures, but are limited in scope to global variables. Subroutines cannot be exported; they can be called only by the procedure (either mainline procedure or subprocedure) in which they are included.




The Modern RPG IV Language
The Modern RPG IV Language
ISBN: 1583470646
EAN: 2147483647
Year: 2003
Pages: 156
Authors: Robert Cozzi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net