fhg
Volume 10, Number 10 -- March 17, 2010

A Good Use for Global Variables

Published: March 17, 2010

by Ted Holt

Using global data (fields, variables and constants) is generally considered to be poor programming practice. One reason is that a subroutine or subprocedure that uses global data is not easily placed into service elsewhere. Another reason is that undesirable side effects may occur when one routine changes a variable that another routine uses. However, in some situations global data makes sense. I would like to provide one example.

Assume a service program with several subprocedures. The source code for the module from which the service program is built looks like this:

H nomain

D/copy prototypes,mysvcpgm

P DoThing1        b                   export
D                 pi             
D  Parm1                        24a   varying const
... more code ...
P                 e

P DoThing2        b                   export
D                 pi             
D  Parm1                        12a   varying
D  Parm2                         9p 0 
D  Parm3                         5a  
... more code ...
P                 e

P DoThing3        b                   export
D                 pi             
D  Parm1                        24a
D  Parm2                        48a
... more code ...
P                 e

... more subprocedures ...

P DoThingN        b                   export
D                 pi             
D  Parm1                         4a
... more code ...
P                 e

The module consists of N (i.e., some number of) related subprocedures that work together to do something (e.g., pricing, costing, shipping, posting, etc.).

Suppose we wish to be able to control these routines in some way. For example, we wish to make them able to use either a 12- or 13-period accounting year. Or we want to make them produce printed output or not. How would we go about controlling the routines?

One way would be to add a parameter to each subprocedure. Let's say we want the subprocedures to produce a program dump when something goes wrong. Each subprocedure gets a new parameter. Let's call the new parameter ParmDump.

H nomain      

D/copy prototypes,mysvcpgm

P DoThing1        b                   export
D                 pi             
D  Parm1                        24a   varying const
D  ParmDump                       n   const
... more code ...
 /free
... more code ...
      monitor;
... more code ...
      on-error;
         if ParmDump;
            dump(a);
         endif;
      endmon;
... more code ...
 /end-free
P                 e

P DoThing2        b                   export
D                 pi             
D  Parm1                        12a   varying
D  Parm2                         9p 0 
D  Parm3                         5a  
D  ParmDump                       n   const
... more code ...
/free
... more code ...
      if SomethingWentWrong;
         if ParmDump;
            dump(a);
         endif;
      endif;
... more code ...
 /end-free
P                 e

P DoThing3        b                   export
D                 pi             
D  Parm1                        24a
D  Parm2                        48a
D  ParmDump                       n   const
... more code ...
/free
... more code ...
      if not %found(SomeFile);
         if ParmDump;
            dump(a);
         endif;
      endif;
... more code ...
 /end-free
P                 e

... more subprocedures ...

P DoThingN        b                   export
D                 pi             
D  Parm1                         4a
D  ParmDump                       n   const
... more code ...
/free
... more code ...
      if Balance < *zero and ParmDump;
         dump(a);
      endif;
... more code ...
 /end-free
P                 e

Each routine has been changed to produce a program dump if something goes wrong and ParmDump is on. Now we must change all the calls in the caller(s) to pass another parameter.

D  DumpOnError                    n
 /free
... more code ...
   if SomeCondition;
      DumpOnError = *on;
   Endif;
... more code ...
   DoThing1 (SomeData: DumpOnError);

The extra parameter in the call to DoThing1 is DumpOnError, which the caller must turn on in order to make the service program produce dumps. What sets SomeCondition? Whatever you want. I'll come back to that in a bit.

Here's another approach that's less burdensome. Rather than add a dump parameter to all N subprocedures, let's add a global variable to the service program to indicate whether errors are to cause dumps or not. Let's add code to each subprocedure to generate a dump when an error is found and DumpEnabled is on.

H nomain    

D/copy prototypes,mysvcpgm

D DumpEnabled     s               n   inz(*Off)

P DoThing1        b                   export
D                 pi             
D  Parm1                        24a   varying const
... more code ...
 /free
... more code ...
      monitor;
... more code ...
      on-error;
         if DumpEnabled;
            dump(a);
         endif;
      endmon;
... more code ...
 /end-free
P                 e
... etc. ...

Now let's add a subprocedure to control the global DumpEnabled variable.

P SetDump         b                   export
D                 pi
D  inSetDump                      n   const

 /free
     eval DumpEnabled = inSetDump;
 /end-free
P                 e

The global variable is DumpEnabled. Notice that it precedes the first subprocedure definition.

Now we modify the calling routine(s), like this:

/free
... more code ...
   if SomeCondition;
      SetDump (*on);
   Endif;
... more code ...
   DoThing1 (SomeData);

Now all subprocedures create dumps when an error arises.

There are plenty of ways to control the condition in the calling routine(s). For example, you might have calling programs check a certain position of a data area. Normally the data area would have a blank in that position, and dumps would not be produced. If the data area contains a D in that position, the caller executes SetDump with an argument of *ON. To control the condition, use the Change Data Area (CHGDTAARA) command.

CHGDTAARA DTAARA(SOMELIB/CONTROL (5 1)) VALUE('D')

In a situation where a group of routines needs common data, global data provides a simple and effective way to make sure that all subprocedures work together.




                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
SEQUEL SOFTWARE

SEQUEL ViewPoint®--Data Access & Analysis
for Power Systems™ Servers

                                               · Easy to use by IT and end users
                                               · Real-time data access and analysis
                                               · Queries, reports, pivot tables, drill-down analysis, kpi dashboards
                                               · IBM i-centric with multiplatform data access
                                               · Expert support and training
                                               · Secure data access
                                               · GUI, Web & green screen interface

SEQUEL--Fast, efficient & cost-effective data analysis

Click here for a FREE Information Kit!


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Erwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

PowerTech:  Strengthen your security. Get a FREE Compliance Assessment today!
New Generation Software:  NGS-IQ: Reporting & BI Software. Easy to Install. Easy to Use.
COMMON:  Join us at the annual 2010 conference, May 3 - 6, in Orlando, Florida


 

IT Jungle Store Top Book Picks

Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
The iSeries Express Web Implementer's Guide: List Price, $49.95
The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
Can the AS/400 Survive IBM?: List Price, $49.00
Chip Wars: List Price, $29.95


 
The Four Hundred
Old Code Meets New Ideas in Latest App Modernization Projects

IBM Cuts Prices for Upgrades to Power 595s

Power 750 Servers Running i Get SAP Benchmarks

Mad Dog 21/21: Mad Dog 21/21: The Teahad Pilot, the Sycophant Senator, and IBM

Internal Disk Arrays Prop Up Storage Sales in Q4

Four Hundred Stuff
System i Automation from RevSoft is 'FAB'ulous

Profound Simplifies Screen Design in Genie Web-Enablement Tool

'Legacy' Integration Blossoms During Recession, Seagull Says

Jack Henry Shop Adopts Banking Framework from IBM

Spinnaker Adds JDE Consulting to 3rd Party Maintenance Business

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
March 13, 2010: Volume 12, Number 11

March 6, 2010: Volume 12, Number 10

February 27, 2010: Volume 12, Number 09

February 20, 2010: Volume 12, Number 08

February 13, 2010: Volume 12, Number 07

February 6, 2010: Volume 12, Number 06

TPM at The Register
VMware opens up Workstation 7.1 beta

Shuttleworth heir opens up on Ubuntu biz

Football U goes to 3Leaf for HPC

Hedge fund suitor denies Novell asset sale rumors

Super Micro to launch AMD render cloud

CA eats Nimsoft cloud watcher

Swedes serve up flicks with KVM

Citrix tunes XenApp for Windows Server R2

Voltaire brings InfiniBand switch to the masses

Cisco 'forever changes internet' with... a router

Terracotta's Ehcache back-ends Hibernate

Tilera wins VC from Broadcom, Quanta, NTT

THIS ISSUE SPONSORED BY:

WorksRight Software
SEQUEL Software
Halcyon Software


Printer Friendly Version


TABLE OF CONTENTS
The State of the UNION

A Good Use for Global Variables

Does An IPL Really Help Improve System Performance?

Four Hundred Guru

BACK ISSUES




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2010 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement