Home > AS/400 Tips > iSeries programmer tips > RPG dynamic value calculation versus hard-coded values
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES PROGRAMMER TIPS

RPG dynamic value calculation versus hard-coded values


Kevin Ketzler, Contributor
08.08.2007
Rating: -3.77- (out of 5)


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


We all know that with few exceptions (e.g. yes/no field) hard-coded valid values shouldn't be used in a program. But how often do you blatantly hard-code processing limits into your RPG programs? The array has 10 positions, so you start a counter at one and write a do loop that runs until it is 10. The warehouse field is defined as P(2,0) in one file and as P(3,0) in another, so you check if the field is greater than 99 and raise an error.

This works until the conditions change. For example, what if the array needs to be changed to 15 positions long? First, you need to change the array definition to 15. Then you have to scan the code, find every time you use it and change the limit to 15. But what if other arrays were defined as 10 positions because this one was defined as 10 positions? You will need to track them down and change them as well.

By defining fields LIKE(other field) and using BIFs like %LEN, %DECPOS, %SIZE and %ELEM, you can create code that will automatically adjust to the changed size. Yes, it means working harder initially, but in the end it will make your life easier when the situation changes. Let me give you an example of dynamic value calculation.

Converting a number to a hexadecimal value string using RPG
This is not a good program, but it is a simple-to-create, good example of dynamic value calculation. It implements a solution to convert a number to a hexadecimal value string. But it is clumsy, relatively slow and uses brute force. The true (read: better) way to convert a number to a hexadecimal value string is to prototype and call the 'C' api CVTHC.

Since this is a compiled program, the PR and PI define the program's entry parameters. By defining the values on the PI explicitly and the values on the PR like the PIs, this allows one set of values to be altered to change the entry parameters. NOTE: Names on the PR are irrelevant but necessary for proper definition.

The hexa data structure is defined to contain the return value as it is being calculated, and to provide a default error value if conversion is not possible. It is defined as containing an array that is filled with "-", which is the error value. The length of the array is defined as the length of the entry parameter return value hex_out. No matter what hex_out is changed to, hexa will contain the same number of characters. In addition, when the program starts it will contain a "-" in each position.

The program checks if the number of hexadecimal positions can store the number passed in, processes its way through those hexadecimal positions and calculates the value of each position (for four char hexadecimal output, the first position is 16^3 for each increment) all based on the length of the hex_out return value.
RPG screenshot of hexadecimal positions, defined parameters for iSeries

Here are the results of calling the program:
RPG hexadecimal positions, defined parameters for iSeries results

Modifying the program

Now to change the program to return a five-character hexadecimal value string, only these two lines need to be changed and the program recompiled.
Dynamic value calculation changed lines

Here are the results of calling the new program:
modified program

ABOUT THE AUTHOR: Ketzler has more than 12 years of Information Systems experience in the iSeries environment and is currently a Sr. Staff Consultant at Affiliated Resource Group. He is an IBM Certified Specialist in AS/400 RPG IV Programming and AS/400 RPG IV Development and can be reached via e-mail at kketzler@aresgrp.com.

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.


Submit a Tip




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



RELATED CONTENT
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

RPG iSeries programming
Enhancing RPG with external SQL stored procedures
Introduction to SQLRPGLE on IBM i: Making a report
Making the most of RPG data handling on IBM i
IBM i shop boosts online sales with RPG-based Web platform
Migrating from RPG to EGL on IBM i
Allow access to data from a stored procedure result set using COBOL or RPG
EGL Rich UI on IBM i: Do you Dojo?
Programming for the Web on the IBM i, what is possible
A taste of COMMON: ILE, IBM releases, Web applications and new products
Documenting nested program structures on the AS/400

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Report Program Generator  (Search400.com)

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