RPGIVbestprac

download RPGIVbestprac

of 11

Transcript of RPGIVbestprac

  • 8/7/2019 RPGIVbestprac

    1/11

    RPG IV Best Pract ices

    Presented by Bryan Meyers

    Copyright 2004, Bryan Meyerswww.bmeyers.net

    Wors t Prac t ices

    I want to use RPG IV, but I dont need ILE

    Ill use RPG IV, but not free format

    I dont have the time

    If it aint broke

    Best Prac t ices

    Enforce RPG IV style standards

    Use free-format RPG IV

    Embrace ILE

    Encourage modular design

    Style Goals

    Accelerate development process

    Write with reuse in mind

    Facilitate program maintenance

    Write with standards in mind

    Reduce errors

    Write smaller, single-function compile units

    Test smaller compile units

    Style Goals

    Eliminate duplicate code

    Even little bits of it

    Use procedures instead

    Eliminate conditional logic

    Hide it in procedures

    Eliminate complex routines

    Hide them in procedures

    Eliminate old code

    Free-Format Speci f ic a t ion

    V5R1 alternative to C-specs

    May be interspersed with C-specs

    ....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8/FREEChain(ne) EmployeeID Employees;Select;When %Error;Return;

    When %Found;Eval(h) TotalPay = (RegHours * Rate) + (OvtHours * Rate * 1.5);

    Endsl;/END-FREE

  • 8/7/2019 RPGIVbestprac

    2/11

    Free-Format Speci f ic a t ion

    Code free-format statements between/FREE and /END-FREE directives

    ....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8/FREEChain(ne) EmployeeID Employees;Select;When %Error;Return;

    When %Found;Eval(h) TotalPay = (RegHours * Rate) + (OvtHours * Rate * 1.5);

    Endsl;/END-FREE

    Column 7....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8/FREEChain(ne) EmployeeID Employees;Select;When %Error;Return;

    When %Found;Eval(h) TotalPay = (RegHours * Rate) + (OvtHours * Rate * 1.5);

    Endsl;/END-FREE

    Free-Format Speci f ic a t ion

    Code goes in columns 8-80

    Columns 6-7 must be blank

    Indenting is allowed

    /FREEChain(ne) EmployeeID Employees;Select;When %Error;Return;

    When %Found;Eval(h) TotalPay = (RegHours * Rate) + (OvtHours * Rate * 1.5);

    Endsl;/END-FREE

    Free-Format Speci f ic a t ion

    Statement can span several lines

    Semicolon ends each logical line

    Multiple statements per line not allowed

    Free-Format Speci f ic a t ion

    Statement begins with opcode

    Opcode Factor1 Factor2 Result;

    /FREEChain(ne) EmployeeID Employees;Select;When %Error;Return;

    When %Found;Eval(h) TotalPay = (RegHours * Rate) + (OvtHours * Rate * 1.5);

    Endsl;/END-FREE

    Free-Format Speci f ic a t ion

    If statement doesnt begin with opcode

    EVAL is assumed for assignment expressions

    CALLP is assumed otherwise

    /FREEEmpNbrErr = *OFF;CalcTaxes(EmployeeID:TotalPay);Eval(h) TotalPay = (RegHours * Rate) + (OvtHours * Rate * 1.5);/END-FREE

    EVAL assumed

    CALLP assumed /FREEEmpNbrErr = *OFF;CalcTaxes(EmployeeID:TotalPay);Eval(h) TotalPay = (RegHours * Rate) + (OvtHours * Rate * 1.5);/END-FREE

    Free-Format Speci f ic a t ion

    Opcode required when extender isnecessary

    E.g., EVAL(H) or CALLP(E)

    EVAL required

  • 8/7/2019 RPGIVbestprac

    3/11

    Free-Format Speci f ic a t ion

    Indicator columns not supported

    No conditioning indicators

    Use structured opcodes instead

    No resulting indicators

    Use BIFs instead

    No level indicators

    Revert to fixed format to specify indicator

    Free-Format Speci f ic a t ion

    Free-format comments begin with //

    May start anywhere from columns 8-80

    Inline comments allowed

    ....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8/FREE// Calculate total pay for employeeChain(ne) EmployeeID Employees;If %Found; // If employee active, calculate total pay

    Eval(h) TotalPay = (RegHours * Rate) + (OvtHours * Rate * 1.5);Endsl;/END-FREE

    Comments

    Free-Format Speci f ic a t ion

    Fixed-format comments may also begin with//

    Anywhere from columns 6-80

    Inline comments not allowed

    CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq// Calculate tax for total transactionC TotTaxable MULT T axRate TaxAmount

    Comment

    Comments

    Convert to // comments

    Eliminate all * comments

    Avoid right-hand "end-line" comments incolumns 81-100

    But use inline // comments where appropriate

    Comments

    Use comments to clarify not echo yourcode

    Brief program or procedure summary

    Title for subroutine, procedure, or other sectionof code

    Explain an obscure technique

    Comments

    Always include a brief summary at thebeginning of a program or procedure

    Program or procedure title

    Brief description of purpose

    Chronology of changes

    Summary of indicator usage (if any)

    Description of the procedure interface

    Example of how to call the procedure

  • 8/7/2019 RPGIVbestprac

    4/11

    Comments

    Use consistent "marker line" comments todivide major sections of code

    Use blank lines to group related source linesand make them stand out

    // ---------------------------- Prototypes

    Declarat ions

    Declare all variables within D-specs

    Organize D-specs consistently

    Main prototype (PR)

    Other prototypes (PR)

    Procedure interface (PI)

    Named constants (C)

    Data structures (DS)

    Standalone variables (S)

    Data St ruct ures

    Indent data item names to improvereadability and document data structures

    Use length notation instead of positionalnotation in data structure declarations

    Use positional notation only when the actualposition in a data structure is important

    When defining overlapping fields, use theOVERLAY keyword instead of positional notation

    Data St ruct ures

    Use qualified data structuresD Customer DS QualifiedD Name 20D YTDSls 11 2D Vendor DS QualifiedD Name 20D YTDPurch 11 2 Dim(12)/FreeCustomer.YTDSls = 0;

    /End-free

    Data St ruct ures

    Avoid multiple occurrence data structures

    Use array data structures instead

    D RegionSls DS Dim(50)D QualifiedD Name 20D MonthlySls 11 2 Dim(12)/FreeRegionSls(Region).MonthlySls(Month) = Totsls;

    /End-free

    Data St ruct ures

    Use LIKEDS, LIKEREC, and externallydescribed data structures

    FCustfile UF E K DiskD CustData DS Likerec(Custrec)

  • 8/7/2019 RPGIVbestprac

    5/11

    Data St ruct ures

    Use data structures instead of key lists

    FCustfile UF E K DiskD CustKey DS Likerec(Custrec:*Key)/FreeChain %KDS(CustKey) Custrec;

    /End-free

    Data St ruct ures

    Avoid compile time arrays

    Provide initial values in a data structure instead

    D DaysData DSD 9 Inz(Sunday)D 9 Inz(Monday)D 9 Inz(Tuesday)D 9 Inz(Wednesday)D 9 Inz(Thursday)D 9 Inz(Friday)D 9 Inz(Saturday)D Days 9 Dim(7)D Overlay(DaysData)

    Naming Convent ions

    Be sure an items name fully and accuratelydescribes the item

    Name lengths of 10 to 14 characters

    User longer names (with ) only whennecessary

    Naming Convent ions

    Name a procedure after its return value

    For subroutines, use a verb/object syntax

    Or for procedures without a return value

    Maintain a dictionary of names, verbs, andobjects

    Naming Convent ions

    Use mixed case to clarify the named itemsmeaning and use

    Avoid using special characters (@, #, $)

    Li tera ls and Constant s

    Use named constants instead of literals

    Except 0 and 1

    D RecordLocked C 01211D FileClosed C 01218/FreeChain(e) %KDS(CustKey) Custrec;Select;When %Status = RecordLocked;

    When %Status = FileClosed;

    Endsl;/End-free

  • 8/7/2019 RPGIVbestprac

    6/11

    Li tera ls and Constant s

    Use named constants instead of literals

    Except 0 and 1

    D RecordLocked C 01211D FileClosed C 01218/FreeMonitor;Chain %KDS(CustKey) Custrec;

    On-error RecordLocked;

    On-error FileClosed;

    Endmon;/End-free

    Ind ica to rs

    Eliminate numbered indicators

    Use named indicators instead

    Use EVAL with *Inxx and *On or *Off insteadof SETON or SETOFF

    Never use MOVEA to set multiple indicators

    Dont use conditioning indicators ever

    Ind ica to rs

    Use functions to replace resulting indicators

    Qualify functions where possible

    Unless used in close proximity to where they areset

    /FreeRead Custfile;If %Eof(Custfile);

    Endif;/End-free

    Ind ica to rs

    Use an indicator data structure with DDSfiles

    FWrkCustD CF E Workstn INDDS(Indicators)D Indicators DSD Exit 3 3ND Cancel 12 12N/FreeIf Exit Or Cancel;

    *Inlr = *Off;Endif;/End-free

    Structured Programming

    Dont use GOTO, CABxx, or COMP

    Dont use obsolete IFxx, DOUxx, DOWxx, orWHxx opcodes

    Perform multipath comparisons withSELECT/WHEN/OTHER/ENDSL

    Dont use CASxx opcode (obsolete)

    Always qualify END opcodes

    Use ENDIF, ENDDO, ENDSL

    Str ing Manipu la t ion

    Use a named constant to declare a stringconstant instead of storing it in an array or

    table

    Avoid using arrays and data structures tomanipulate character strings and text

    Use EVAL whenever possible for stringmanipulation

  • 8/7/2019 RPGIVbestprac

    7/11

    Avoid Obsolescenc e

    Dont sequence program line numbers incolumns 1-5

    Avoid program-described files

    Use free-format syntax for opcodes

    If a function and an operation code do thesame thing, use the function

    Avoid Obsolescenc e

    Shun obsolete operation codes

    ADD Use + operatorDIV Use / operator or %DIVMULT Use * operatorMVR Use %REMSQRT Use %SQRTSUB Use operatorXFOOT Use %XFOOTZ-ADD Use EVALZ-SUB Use EVAL

    Avoid Obsolescenc e

    Shun obsolete operation codes

    CAT Use + operatorCHECK Use %CHECKCHECKR Use %CHECKRSCAN Use %SCANSUBST Use %SUBSTXLATE Use %XLATE

    Avoid Obsolescenc e

    Shun obsolete operation codes

    MOVE Use %SUBST, expressions, BIFsMOVEA Use %SUBST, expressions, BIFsMOVEL Use %SUBST, expressions, BIFs

    Avoid Obsolescenc e

    Shun obsolete operation codes

    ANDxx Use ANDCABxx Use expressionsCASxx Use SELECT/WHEN/OTHERCOMP Use expressionsDO Use FORDOUxx Use DOUDOWxx Use DOWIFxx Use IFORxx Use ORWHENxx Use WHEN

    Avoid Obsolescenc e

    Shun obsolete operation codes

    CALL Use CALLPCALLB Use CALLPPARM Use PR/PI definitionsPLIST Use PR/PI definitions

  • 8/7/2019 RPGIVbestprac

    8/11

    Avoid Obsolescenc e

    Shun obsolete operation codes

    ADDDUR Use + operator, date BIFsEXTRCT Use %SUBDTSUBDUR Use operator, date BIFs

    or use %DIFFTIME Use date BIFs

    Avoid Obsolescenc e

    Shun obsolete operation codes

    ALLOC Use %ALLOCDEFINE Use D-specs with LIKEKFLD Use %KDS data structure (V5R2)KLIST Use %KDS data structure (V5R2)LOOKUP Use %LOOKUP or %TLOOKUPOCCUR Use array data structure (V5R2)REALLOC Use %REALLOCSETOFF Use expressionsSETON Use expressionsSHTDN Use %SHTDN

    Avoid Obsolescenc e

    Shun obsolete operation codes

    BITOFF Use %BITAND, %BITNOT (V5R2)BITON Use %BITOR (V5R2)MHHZO Use %BITAND, %BITOR (V5R2)MHLZO Use %BITAND, %BITOR (V5R2)MLHZO Use %BITAND, %BITOR (V5R2)MLLZO Use %BITAND, %BITOR (V5R2)TESTB Use %BITAND (V5R2)TESTN Use MONITOR, ON-ERRORTESTZ Use %BITAND (V5R2)

    Avoid Obsolescenc e

    Shun obsolete operation codes

    GOTO No alternativeTAG No alternative

    Avoid Obsolescenc e

    Dont mix free format with fixed format C-specs

    CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++/FREE// Calculate total pay for employeeEmpNbrErr = *OFF;

    /END-FREEC MOVEL(P) CpyNbr EmployeeIDC MOVE EmpNbr EmployeeID/FREEChain(ne) EmployeeID Employees;

    /END-FREE

    Misce l laneous Guide l ines

    In all specifications that support keywords,observe a one-keyword-per-line limit

    Leave column 7 blank in H- and D-specs

  • 8/7/2019 RPGIVbestprac

    9/11

    Modular Programm ing

    Use prototypes to define parameters andprocedure interfaces

    Even *ENTRY parameter lists

    // --------------------------- Main procedure interfaceD Main PR EXTPGM('MYTRGPGM')D TrgBuffer 256D TrgBufferLen 10I 0D Main PID TrgBuffer 256D TrgBufferLen 10I 0

    Modular Programm ing

    Store procedure prototypes in /COPYmembers

    Or use conditional compilation

    Include constant declarations in the same/COPY member as a modules prototypes

    Use IMPORT and EXPORT only for globaldata items

    Modular Programm ing

    Use binding directories consistently

    *Combination of application-specific bindingdirectory and cross-application binding directory

    Stick to a minimum number of bindingdirectories

    CRTPGM PGM(MYPGM) BNDDIR(APPBNDDIR GENBNDDIR)

    Modular Programm ing

    Control service program signatures withbinder language

    STRPGMEXP PGMLVL(*CURRENT)EXPORT SYMBOL(DAYNBR)EXPORT SYMBOL(DAYNAME)

    ENDPGMEXPSTRPGMEXP PGMLVL(*PRV)

    EXPORT SYMBOL(DAYNBR)ENDPGMEXP CRTSRVPGM SRVPGM(DATSRVPGM)MODULE(*SRVPGM)

    EXPORT(*SRCFILE)SRCFILE(QSRVSRC)SRCMBR(*SRVPGM)

    Modular Programm ing

    Avoid running ILE programs in the defaultactivation group

    DFTACTGRP(*NO)

    ACTGRP(name)

    ACTGRP(*CALLER) during OPM-ILE transition

    Modular Programm ing

    Beware AG-related problems

    Invalid override scope

    Invalid shared ODP scope

    RCLRSC errors

  • 8/7/2019 RPGIVbestprac

    10/11

    Modular Programm ing

    Convert reusable code to procedures Redundant routines, subroutines

    Business-specific processing

    /COPY members

    Called programs

    Modular Programm ing

    Store reusable *MODULE objects in acommon library Use consistent naming conventions

    Use public documentation

    The Last Word on Sty le

    Once and only once

    Lots of little pieces

    Make it right before you make it faster Keep it right when you make it faster

    Make it clear before you make it faster

    Dont sacrifice clarity for small gains inefficiency

    Read More About I t

    iseriesnetwork.com

    bmeyers.net

    Self-pac ed DVD Train ing

    iseriesnetwork.com

    bmeyers.net

    iSer ies Netw ork e -Learn ing

    iseriesnetwork.com

    bmeyers.net

  • 8/7/2019 RPGIVbestprac

    11/11

    Quest ions?

    bmeyers.net