• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • If the Compiler Can’t Find the Mistake, Maybe You Can

    August 22, 2007 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. If 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

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    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
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    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

    New Jersey Hospital Picks HMS for Healthcare Apps CA Extends Change Management to i5/OS

    Leave a Reply Cancel reply

Volume 7, Number 30 -- August 22, 2007
THIS ISSUE SPONSORED BY:

WorksRight Software
Help/Systems
Krengeltech

Table of Contents

  • If the Compiler Can’t Find the Mistake, Maybe You Can
  • Commands with Generic Parameters
  • Troubleshooting NetServer File Copy Errors

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle