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 value


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


RELATED CONTENT
iSeries programmer tips
Groovy programming on IBM i
EGL Rich UI on IBM i: Do you Dojo?
Running PHP open source applications: NOBODY needs authority
Programming for the Web on the IBM i, what is possible
Using geocoding on AS/400 to enhance your Web presence
The best technologies and tools for System i programmers in 2009
Seven IBM i project lessons learned in 2008
Documenting nested program structures on the AS/400
What is an integrated database?
An automated CL method of moving a query from AS/400 to Excel

RPG iSeries programming
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
How to: Sort arrays using RPGIV
How to: Create an RPGLE array
How to use an embedded SQL statement and display the result in a subfile
Eight steps for creating program documentation using AS/400 utilities

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


s 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.
[IMAGE]

Here are the results of calling the program:
[IMAGE]

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.
[IMAGE]

Here are the results of calling the new program:
[IMAGE]

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




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