Don't proliferate old, bad code

In an effort to hasten the completion of projects, most programmers "steal" code from other sources within their libraries. The problem is that many times, the code originated years ago and is cumbersome, lengthy, and not easy to maintain. Even most ILE code resulted from old code that was passed through a conversion utility. Using the functionality of RPGIV's Keywords and Built-In-Functions usually can make the code much smaller and manageable. Here are just a couple of examples that I've come upon recently.

Example 1:
A file contains many fields used as buckets, such as monthly totals, and each field needs to be interrogated.

Define a Data Structure with each file field as a subfield and define an array and move data structure to array.

 FINPUTFIL  IF   E           K DISK
D ARY             S             10    DIM(4)
D FILDS           DS            40 
D  FLD01                  1     10 
D  FLD02                 11     20 
D  FLD03                 21     30 
D  FLD04                 31     40

.... Move each Field to array and do processing
Do a rename of each field.

I              FLD01                       ARY(1)
I              FLD02                       ARY(2)
I              FLD03                       ARY(3)
I              FLD04                       ARY(4)

Define External Data Structure and Overlay Array on Data Structure

FINPUTFIL  IF   E           K DISK
D FILDS         E DS            40    EXTNAME(INPUTFIL)
D  ARY                    1     40    DIM(4)
.... Now ARY(1) = FLD01, etc. automatically 

Example 2:
Date Validation.

Use Data Structures and moves to check each field. This example was passed on to many different programs.

I            DS                                      
I                                        1   60DSPDAT 
I                                        1   20$@M   
I                                        3   40$@D   
I                                        5   60$@Y   
C           $@D       CABLT1         $TG001       11 
C           $@M       CABGT12        $TG001     11   
C           $@M       CABLT1         $TG001       11 
C           $@Y       DIV  4         $@REM   10      
C                     MVR            $@REM           
C                     Z-ADD$@M       $@MS    20      
C           $@REM     IFEQ 0                         
C           $@M       IFEQ 2                         
C                     Z-ADD13        $@MS         
C                     END                         
C                     END                         
C           $@D       CABGT$@E,$@MS  $TG001     11 

Use ILE Date operations and edits.

D Today           S               D   Inz(*SYS)
D WrkDate         S               D
D Days            S              4  0
C     *MDY          Test(DE)                DSPDAT
C                   If        %Error    
C                   Eval      *In81 = *On
C                   Endif                            
C     *MDY          Move      DSPDAT        WrkDate
C     WrkDate   SubDur     Today         Days:*D
C                   If        Days < *Zero
C                   Eval      *In81 = *On            
C                   Endif 

Example 3:
Building text strings.

The most common examples include using arrays with loops that test for each character or builds an array of characters and moves the array to a text field.

Using BIFs like %Editc, %Subst, %Scan, %Trim, %Xlate will allow complex text strings to be validated and built with as little as one eval statement through concatenation.

DExcCmd           PR                  ExtPgm('QCMDEXC')
D Command                      320    Const            
D Length                        15  5 Const            
C                   Eval      Clock# = %Subst(User:3:5)                   
C                   Eval      #UsrDta = %Subst(Pm1CLASS:1:2) + '-' +      
C                              %Trim(%Editc(JobTckt#:'Z'))                
C                   Eval      #SplNm = %Subst(DayOfWeek:1:3) +            
C                              PullHouse                                  
C                   Eval      Command = 'OVRPRTF BCLABELS OUTQ(' + Outq + 
C                              ') HOLD(*YES) SAVE(*NO) SPLFNAME(' +       
C                              #SplNm + ') USRDTA(''' + #UsrDta + ''')'   
C                   Eval      WrkClass = Pm1CLASS                         
C                   Callp     ExcCmd(Command:Length) 

Moral of the story: Use the full capabilities of RPGIV with Keywords and Bifs, especially when taking old code and re-using it.


The Best Web Links: tips, tutorials and more.

Ask your systems management questions--or help out your peers by answering them--in our live discussion forums.

This was first published in March 2002

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.