25
Thu, Apr
0 New Articles

Procrastinator's Guide to Programming Why Wait?

General
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

The way companies do business is constantly evolving in response to internal and external changes. For this reason, the software that supports your business environment must also evolve or face obsolescence. The more flexible your software is, especially in areas that are likely to change, the less likely that it will become obsolete.

One area likely to change is the edits that are applied to determine the validity of business transactions. Most of the time, changing the way a value is edited in a program or setting up a new default value involves a simple programming task. When this simple task has to be repeated dozens of times, you might begin to wonder if a better way to derive defaults or edit values exists.

One solution is to avoid duplicating these functions in your programs by encapsulating them in programs or procedures. To make these encapsulated functions even more flexible, you can use a dynamic call that allows a program to wait until runtime to determine which program or procedure is called. Waiting until runtime allows you to change a program’s behavior without recompiling the program.

Throughout this article, I refer to dynamic program calls and dynamic procedure calls. The AS/400 waits until a program or service program is first called to resolve the location of a program or service program’s procedures, even when the program or procedure cannot change without recompiling the calling program. Because the location is resolved at runtime, these types of calls are sometimes referred to as dynamic calls. For the purposes of this article, I use the term dynamic call to indicate that the program or procedure being called is stored in a program variable and can change each time the program is run without recompiling.

You Make the Call

In Original Program Model (OPM) programs, the only way you could wait until runtime to determine which program was called was to store the name of the called program in a program variable. Although you still have this capability in ILE programs, ILE provides you with another alternative: a dynamic procedure call. Dynamic procedure calls let you plug in functions at runtime. Dynamic procedure calls have several advantages compared to dynamic program calls, including better performance, less program initialization overhead, and the ability to use the more flexible, prototyped call interface.

AS/400 programs have always been able to call another program if you’ve placed that program name in a variable. Typically, the calling program retrieves the name of the program being called from a file or data area and uses a variable to call the program using the CALL op code or CALL command. Because the program making the call has no way of knowing which program will be called, information about the program being called is resolved on each call. The system overhead to support this type of call is higher than for any other type of call. Because you might require hundreds of edits to support the validation of a typical business transaction, this type of call can be impractical.

A better-performing alternative to a dynamic program call—the dynamic procedure call—can be made by using a prototype. The prototype allows you to define a return value for a subprocedure. Prototypes also allow parameters to be omitted, passed by value, or passed as constants.

The use of dynamic calls also has some drawbacks. The biggest disadvantage is that most of the documentation and cross-reference tools will not be able to identify and catalog what is being called. For this reason, it is important to document your use of dynamic calls. The other major drawback is that dynamic procedure calls are more complex and require additional steps. Fortunately, you can hide a lot of this additional complexity within subprocedures that encapsulate some of the steps required to support dynamic procedure calls.

Dynamic Procedure Calls

Any subprocedure that can be called using a static procedure call can also be called dynamically using a procedure pointer. Static and dynamic procedure calls have almost identical impact on system overhead, making it practical to use dynamic procedure calls to support call-intensive functions like transaction editing. If you use a prototype to define the interface used for your dynamic procedure call, you can use a return value. Return values allow you to treat the called procedure like a data element, which can make your program easier to understand.

Before you can make a dynamic procedure call, you must have a procedure pointer, which you can retrieve in two ways. The first method employs the %addr built-in function to retrieve the address of the procedure being called. This method requires that the called procedure be defined in the same module as the procedure making the call. (See “Is There an Echo in Here?” MC, August 1999, to learn how this technique works.)

The second method uses APIs to activate the service program containing the procedure being called and to retrieve a pointer to the procedure being called. This is the approach I used in the transaction edit example that follows.

Before you can call a procedure dynamically, you must know the name of the service program that contains the subprocedure. In the example, the service program procedure names are stored in a keyed file. The first time the service program is used, you need to activate it using several APIs. Once the service program is activated, you can retrieve pointers to the service program’s exported procedures and exported data elements using another API.

You activate a service program by calling the Activate Bound Program (QLEACTBNDPGM) API. One of the parameters passed to this API is a system pointer to the service program. I use the C MI library function, Resolve System Pointer (RSLVSP), to set the system pointer passed to the Activate Bound Program API. One of the parameters passed to the RSLVSP API is the hex object type of the service program. Although you could hardcode the hex type for a service program, the example uses the Convert Type (QLICVTTP) API to retrieve the hex type.

To call a subprocedure dynamically, you need to specify a procedure pointer on the prototype for the subprocedure using the EXTPROC keyword. To define a pointer as a procedure pointer, use an asterisk (*) for the data type with the PROCPTR keyword.

Figure 1 contains an example that shows the prototype for a dynamically called subprocedure.

You’ve Found Your Calling

Dynamically called subprocedures have their advantages when you use them to support areas of your applications that change frequently. Areas particularly suited to their use include the retrieval of default field values, validation of field values, support for standard I/O functions, and support for application exit points.

The example I have included shows how you can use dynamic procedure calls to retrieve default values and edit displayed prompt values. The TstDyn program makes dynamic calls to subprocedures contained in the DynCallS service program. These procedures retrieve default values and provide edits for the displayed fields. The names of the subprocedures to call and their associated service program (DynCallS) are stored in files EdtTbl and DftTbl. Each file has a logical built over it, keyed by display file, format, and field. You can download the code and installation instructions for the dynamic edit example using the download link at the Midrange Computing Web site (www.midrangecomputing.com/mc/).

When the DynCall example is first called, it checks to see if any fields that it displays have a dynamically called default value subprocedure. If it detects any, they are called to retrieve the default values. The screen is then displayed. When a user presses Enter, the DynCall program checks for errors by dynamically calling any edit subprocedures for the input-capable fields being displayed. These edit subprocedures validate the data that was entered; if one of them detects an error, it sends an error message and returns an error flag indicating the field in error.

After you download and compile this code, you might want to experiment by adding additional edits to the fields. To do this, copy one of the existing edit subprocedures and give it a new name. You can place this new subprocedure in the DynCall module and service program or create a new module and/or service program. Next, enter the code for your new edit and compile the module and service program that will contain the new edit. After the service program has been compiled, add a record to the EdtTbl file specifying the name of the new subprocedure, service program, display file, format, and field.

You can use the code in this example as a basis for creating a set of subprocedures that support editing of file field values. One area not addressed in this example involves edits that depend upon multiple values. You can deal with this very common situation in several ways. One way you can make related information available to your edit procedure is to post your application field values in a global area that your application and dynamically called subprocedures can access. The local data area (*LDA) is an example of such an area. An alternative that I recommend is to register your program and data file values by name using a subprocedure. I use a subprocedure to store the address of display and data file fields by name in a user index and user space located in library QTEMP.

Last Call

This example demonstrates how dynamic procedure calls allow you to wait until runtime to decide what is called. Waiting until runtime can make it easier to change your applications to fit new requirements. Using this technique, you can even change the behavior of an application as it runs! This technique offers another benefit in that it facilitates the integration and sharing of subprocedures that support commonly used functions into your applications.

Reference

“Is There an Echo in Here?” John V. Thompson, MC, August 1999.

Related Materials

• ILE RPG for AS/400 Programmer’s Guide (SC09-2507-01, CD-ROM QB3AGY01)
• MI Library Reference (SC09-2418-00, CD-ROM QBJADR00)
• OS/400 Object APIs V4R3 (SC41-5865-02, CD-ROM QB3AMQ02)
• OS/400 Program and CL Command APIs V4R3 (SC41-5870-02, QB3AMV02)

**************************************************************************

* DynEdtVal Prototype used to dynamically call subprocedures that *

* edit a field’s value. *

* Input: *

* pFld Pointer to the fields value. *

* Return: *

* RtnErr Error flag indicating field is in error. *

**************************************************************************

DpDynEdtVal S * PROCPTR
DDynEdtVal PR N EXTPROC(pDynEdtVal)
D PR_pFld * VALUE
D PR_DspF 10A CONST
D PR_Fmt 10A CONST
D PR_Fld 10A CONST

...

* Retrieve procedure pointer
C EVAL pDynEdtVal = RtvExpPrcPtr(
C ETSrvPgm:
C ETSubPrc)
* Dynamically call editing sub-procedure.
C EVAL RtnErr = DynEdtVal(pFld: DspF: Fmt: Fld)

Figure 1: Here’s the prototype and dynamic call for the called editing procedure.

DAVID MORRIS
David Morris has worked with and written about a variety of technologies, including ILE, RPG, business intelligence, SQL, security, and genetic programming. Today, David is developing Web applications that run on the iSeries using RPG, Java, and XML as well as writing about these technologies for technical journals.

MC Press books written by David Morris available now on the MC Press Bookstore.

 

XML for eServer i5 and iSeries XML for eServer i5 and iSeries

In this book, you will learn about Extensible Markup Language (XML), but with an IBM eServer i5/iSeries twist.

List Price $64.95
Now On Sale
 
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: