fhg
Volume 7, Number 30 -- August 22, 2007

If the Compiler Can't Find the Mistake, Maybe You Can

Published: August 22, 2007

by Ted Holt

Programs should not break when people change them, yet they often do. Sometimes programs break when programmers modify only one of two things that have to stay in sync. Compilers can't always catch such errors, but you often can.

For example, suppose that you are developing a program with a control break. Regardless of what language you are using, you will need a holding variable for each control field in order to test when a control field changes. Here's an example of such a variable.

D SaveItemNo      s                   like(ItemNo)

ItemNo is a database field. SaveItemNo is defined to be like ItemNo. It the size of ItemNo ever increases, the size of SaveItemNo will also increase, and the control break logic will continue to work correctly.

However, suppose the original programmer did not use the like keyword, but defined SaveItemNo to have the appropriate length and decimal positions.

D SaveItemNo      s              7a                     

A maintenance programmer would have to modify the definition of SaveItemNo whenever the definition of ItemNo changed. If the programmer defined ItemNo without redefining SaveItemNo, the program would probably not work correctly.

Here's another example, in which the GrandTotal variable is defined for the summing up a number.

D GrandTotal      s             +2    like(Total)

GrandTotal is defined to be two digits larger than Total, with the same number of decimal positions. No matter how often Total is redefined, GrandTotal will always be two digits larger than Total.

The like keyword is a great way to ensure that data definitions stay in sync. By the way, DDS and COBOL also have corresponding methods of defining data to be like other data.

But the compiler cannot check all such dependencies. Look at this example:

D Company         s              2a                     
D Customer        s              5a                     
 // Key must be as long as Company and Customer together
D Key             s              7a                     

C                   movel     Company       Key 
C                   move      Customer      Key 
C     Key           chain     SomeFile 

I have seen many programs with code like this. The developer has thoughtfully included a comment that explains how Key is defined in relation to the Company and Customer fields. But what if a maintenance programmer doesn't see this comment?

In this case, there is no way to tell the compiler that Key must be as large as Company and Customer together. But you can enforce the dependency yourself. RPG IV includes built-in functions that access data definition.

Here's the same code, with an assertion that verifies that Key is defined correctly.

H dftactgrp(*no) actgrp(*new)                                         
H bnddir('TOOLKITBD')                                                 
                                                                      
D/copy prototypes,assert                                              
                                                                      
D Company         s              3a                                   
D Customer        s              5a                                   
 // Key must be as long as Company and Customer together              
D Key             s              7a                                   
                                                                      
 /free                                                                
     *inlr = *on;                                                     
     assert (%size(Company) + %size(Customer) = %size(Key):           
             'Key must be as long as Company and Customer combined'); 
/end-free                                                            
C                   movel     Company       Key                       
C                   move      Customer      Key                       
C     Key           chain     SomeFile 
 /free                                                                
     return;                                                          

If you're not familiar with assertions, you may want to read Cletus the Codeslinger's article about that very topic. If someone redefines Company or Customer, but does not refine Key accordingly, the program will rudely cancel upon first invocation (which will be in a test environment!) The program will not run until the dependency is fixed. You don't have to use an assertion, of course. Any mechanism that alerts someone to the problem is OK.

Here's an example from a program I wrote recently. There are two arrays--Data and Attr. In this program, it is imperative that the two arrays have identically-defined elements, but Data must have two elements more than Attr.

H dftactgrp(*no) actgrp(*new)                                      
H bnddir('TOOLKITBD')                                              
                                                                   
D/copy prototypes,assert                                           
                                                                   
D*DataDim         c                   const(6)                     
D DataDim         c                   const(8)                     
D Data            s             40a   dim(DataDim)                 
D                                                                  
D AttrDim         c                   const(4)                     
D Attr            s                   like(Data) dim(AttrDim)      
                                                                   
 /free                                                             
     *inlr = *on;                                                  
     assert (%elem(Data) - %elem(Attr) = 2:                        
             'Array Attr must have exactly 2 fewer elements than + 
              array Data');                                        

Again, an assertion placed at the beginning of the calculation specs prevents the program from running if either array's dimension is changed without the other array's being adjusted accordingly.

Here's one last example. Packed variables SomeQty and SomePct are supposed to have the same number of digits, but SomePct, which is a percentage, must have two more decimal places. The assertion verifies that this is so.

H dftactgrp(*no) actgrp(*new)                                 
H bnddir('TOOLKITBD')                                         
                                                              
D/copy prototypes,assert                                      
                                                              
 // SomeQty and SomePct must have the same number of digits,  
 // but SomePct must have 2 more decimal positions.           
D SomeQty         s              7p 2                         
D SomePct         s              7p 4                         
                                                              
                                                              
 /free                                                        
     *inlr = *on;                                             
     assert (%size(SomeQty)=%size(SomePct) and                
             %decpos(SomePct)-%decpos(SomeQty)=2:             
             'Invalid definition of SomeQty and/or SomePct');

It's a standing joke in many shops that any program that compiles must be correct. We all know that that's not necessarily the case. Since the compiler can only go so far, it's up to developers to add strength to the programs our employers depend on.


RELATED STORY

Programming with Assertions



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


Sponsored By
WORKSRIGHT SOFTWARE

Do you need area code information?
Do you need ZIP Code information?
Do you need ZIP+4 information?
Do you need city name information?
Do you need county information?
Do you need a nearest dealer locator system?

We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

Just call us and we'll arrange for 30 days FREE use of either
ZIP/CITY or PER/ZIP4.

WorksRight Software, Inc.
Phone: 601-856-8337
Fax: 601-856-9432
E-mail: software@worksright.com
Web site: www.worksright.com


Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
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

COMMON:  Join us at the annual 2008 conference, March 30 - April 3, in Nashville, Tennessee
Computer Keyes:  KeyesOverlay rapidly converts standard *SCS printer files into PDF documents
Bytware:  Simplify and strengthen your System i security with object-based solutions


 

IT Jungle Store Top Book Picks

The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket Developers' 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
iSeries Express Web Implementer's Guide: List Price, $59.00
Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
WebFacing Application Design and Development Guide: List Price, $55.00
Can the AS/400 Survive IBM?: List Price, $49.00
The All-Everything Machine: List Price, $29.95
Chip Wars: List Price, $29.95


 
The Four Hundred
i5/OS V6R1: The TIMI, It Is A-Changing

Solaris Coming to the System i?

The System i Gets Price Changes and Withdrawals

As I See It: Of Toads and Time

The Linux Beacon
Intel Cranks Out Two More Quads, AMD Sets Barcelona Date

Tilera Launches 64-Core, Linux-Based Mesh Processor

Citrix Buys Virtualization Challenger XenSource for $500 Million

Court Says Novell Owns Unix, Not SCO

Four Hundred Stuff
Is PHP the Systems i's Next RPG?

Notes/Domino 8 Hits the Streets

450,000-Line RPG App Converted to .NET in Six Months

CA Extends Change Management to i5/OS

Big Iron
Solaris Unix Is Coming to IBM Mainframes

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
August 11, 2007: Volume 9, Number 32

August 4, 2007: Volume 9, Number 31

July 28, 2007: Volume 9, Number 30

July 21, 2007: Volume 9, Number 29

July 14, 2007: Volume 9, Number 28

July 7, 2007: Volume 9, Number 27

The Windows Observer
AMD Gooses Dual-Core Opteron Speeds, Cuts Prices

IIS Gains More Web Server Market Share, Says Netcraft

NEC Brings Linux, Windows Clustering Software to North America

Unisys First Up with TPC-E Benchmark Test

The Unix Guardian
Sun Polishes Up Sparc T2 Multithreaded Chips

AMD Gooses Dual-Core Opteron Speeds, Cuts Prices

Sun Creates Virtual Tape Library from Thumper Server

As I See It: Policeware

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

THIS ISSUE SPONSORED BY:

WorksRight Software
Help/Systems
Krengeltech


Printer Friendly Version


TABLE OF CONTENTS
If the Compiler Can't Find the Mistake, Maybe You Can

Commands with Generic Parameters

Troubleshooting NetServer File Copy Errors

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
Rexx STDIN File

What is holding companies back from using CASE tools?

Hexadecimal thumbprint of a file

Duplicated printer files

Problem with "cpyfrmimpf"





 
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-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement