Home > AS/400 Tips > iSeries programmer tips > Don't proliferate old, bad code
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES PROGRAMMER TIPS

Don't proliferate old, bad code


Joe Szymanski
03.19.2002
Rating: -3.94- (out of 5) Hall of fame tip of the month winner


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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.



Code

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

OLD WAY:
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
C     FILKEY        CHAIN     INPUTFIL 

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

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

NEW WAY:
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)
C     FILKEY        CHAIN     INPUTFIL
.... Now ARY(1) = FLD01, etc. automatically 

Example 2:
Date Validation.

OLD WAY:
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 

NEW WAY:
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.

OLD WAY:
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.

NEW WAY:
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.

==================================
MORE INFORMATION ON THIS TOPIC
==================================

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.


Rate this Tip
To rate tips, you must be a member of Search400.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
iSeries CL programming
Taking advantage of CL advancements, starting with V5R3
Checking in on your IBM i authorization lists
Running PHP open source applications: NOBODY needs authority
Simplify the process of converting a spool file from iSeries into an Excel spreadsheet
CL program for daily backups
An automated CL method of moving a query from AS/400 to Excel
Changing user password expiration
Eight steps for creating program documentation using AS/400 utilities
DAYSPAST CLLE program for AS/400: Compares object creation date with today's date
Advanced Job Scheduler help

iSeries programmer tips
Enhancing RPG with external SQL stored procedures
Tracking data changes on IBM i with triggers
Introduction to SQLRPGLE on IBM i: Making a report
Implementing a browser interface in COBOL: Displaying database fields
Taking advantage of CL advancements, starting with V5R3
TAATOOL: Useful tools for programmers on IBM i
Implementing a browser interface in COBOL: Creating your graphic Web page
Implementing a browser interface in COBOL: Getting started
Making the most of RPG data handling on IBM i
Groovy programming on IBM i

Application Development
iSeries calling an .exe
Top 10 programmer tips
Formatted work job scheduler
Convert system date and time
Mixing free format code with embedded SQL
SQL update a field in one file from a field in another file
Webcasts for iSeries programmers
Programming advice from the pros
Easy code copying via the drag and drop method
Setting FTP time-outs

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

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.



iSeries Security - Security Tools, Physical Security and System Security
HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts