• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Dealing with Faulty Logic

    October 19, 2011 Ted Holt

    Supposedly computer programming is a logical science. One would think, then, that computer programmers would be logical thinkers. It is not so. Let me provide a few examples of illogical thinking taken from real production programs. Then I will show a simple, low-tech way to deal with logical expressions so that they don’t end up illogical.

    Let’s start with this example taken from fixed-format RPG specs. I suggest you find the bad logic yourself before reading my comments.

    TYPE          IFNE      'A'
    TYPE          IFEQ      'S'
    XCODE         CABNE     'S'           CONTIN
                  ENDIF
    TYPE          IFEQ      *BLANK
    XCODE         CABEQ     'S'           CONTIN
                  ENDIF
                  ENDIF
    

    This code carries out the programmer’s intention. However, the first IF and its corresponding ENDIF are unnecessary.

    Here’s another one.

                 PGM
    START:
                 CALL       PGM(PGM1)
                 CALL       PGM(PGM2)
                 CLRPFM     FILE(SOMELIB/SOMEFILE)
                 GOTO END
                 GOTO START
     END:
                 ENDPGM
    

    The second GOTO will never take place. Any unlabeled calculation that follows an unconditional branch will never run, and some language compilers flag such as an error. The CL compiler, however, doesn’t mind a bit, and won’t even generate a warning message.

    Here’s a third example, before I move on to something more meaty.

    If        WarehouseType = 'C'
    Eval      Balance = Quantity
    Else                     
    Eval      Balance = Quantity
    Endif
    

    It really doesn’t matter if the warehouse is controlled or uncontrolled, does it?

    Now let’s look at a more complicated logic error.

    If       Balance > SkidQty and
             SkidQty > 1 and OrderType <> 'Z'
             or  ShipmentType = '1'  and 
             SkidQty > 1 and OrderType <> 'Z'
             and Balance > SkidQty     
    

    This one is a bit more difficult to unravel, and most likely, the code does not carry out the programmer’s intentions.

    Here’s an easy way to deal with complex conditions. You’ll need two high-tech implements–a pencil and a sheet of paper. You will use the pencil to draw a diagram of the logical expression on the paper.

    First, here’s what an AND looks like:

    And here’s what an OR looks like:

    You can swap expressions within a single AND or OR. The following two expressions are identical.

    As are these:

    (There is one exception, however. If short-circuit evaluation applies, and an expression modifies a variable through a call to a subprocedure, then reordering the expressions may change the logic of the program. I prefer to avoid such logic, as it is unreliable and difficult to understand. See ShortCircuit Evaluation for more details.)

    And you can mix ANDs and ORs, like this:

    Before we diagram this expression, let’s reformat it to be sure we get it right. The system evaluates ANDs before ORs, so I add parentheses for clarification.

    If (Balance > SkidQty and SkidQty > 1 and OrderType <> 'Z')
    or (ShipmentType = '01' and SkidQty > 1 and OrderType <> 'Z'
             and Balance > SkidQty)
    

    Now, let’s diagram this expression:

    Since some of the conditions are in both branches of the OR, let’s align them:

    It’s clear that the value of ShipmentType is irrelevant. This complex condition is probably not accomplishing what the programmer intended.

    Here are a few tips for dealing with complex conditions.

    1. Add parentheses for clarification, as I did in the last example. The parentheses may be unnecessary, but their presence ensures that the computer evaluates the expressions in the order you want them evaluated, and clarifies your intentions to anyone who reads the program.
    2. Diagram the logic using the method I’ve illustrated or some other method that you prefer before writing the source code.
    3. Add comments that express your intentions. Concentrate more on why than what.

    To expand on the last point, my experience is that comments tend to be worthless, and sometimes even misleading. Here is a worthless comment from a real production program.

    This line prints when there are no errors and when there are errors.
    

    I have no idea what the programmer was thinking.

    A common misconception is that programming is equivalent to writing source code. Nothing could be further from the truth. Writing source code is only part of the programming process. A mistake people make (and one that I myself have made all too often) is to begin writing source code without first working out the solution to the problem at hand. The result is the type of code I’ve shown above.

    Do what Thomas J. Watson, Sr., was urging IBM employees to do decades ago. Think.

    RELATED STORY

    ShortCircuit Evaluation



                         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
    DRV Tech

    Get More Out of Your IBM i

    With soaring costs, operational data is more critical than ever. IBM shops need faster, easier ways to distribute IBM applications-based data to users more efficiently, no matter where they are.

    The Problem:

    For Users, IBM Data Can Be Difficult to Get To

    IBM Applications generate reports as spooled files, originally designed to be printed. Often those reports are packed together with so much data it makes them difficult to read. Add to that hardcopy is a pain to distribute. User-friendly formats like Excel and PDF are better, offering sorting, searching, and easy portability but getting IBM reports into these formats can be tricky without the right tools.

    The Solution:

    IBM i Reports can easily be converted to easy to read and share formats like Excel and PDF and Delivered by Email

    Converting IBM i, iSeries, and AS400 reports into Excel and PDF is now a lot easier with SpoolFlex software by DRV Tech.  If you or your users are still doing this manually, think how much time is wasted dragging and reformatting to make a report readable. How much time would be saved if they were automatically formatted correctly and delivered to one or multiple recipients.

    SpoolFlex converts spooled files to Excel and PDF, automatically emailing them, and saving copies to network shared folders. SpoolFlex converts complex reports to Excel, removing unwanted headers, splitting large reports out for individual recipients, and delivering to users whether they are at the office or working from home.

    Watch our 2-minute video and see DRV’s powerful SpoolFlex software can solve your file conversion challenges.

    Watch Video

    DRV Tech

    www.drvtech.com

    866.378.3366

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Townsend Security:  View the recorded Webcast: Secure Managed File Transfers for the IBM i
    Dan Riehl Presents:  Fall Training Sale – Discounts up to 40%! RPG IV COBOL CL Admin Security
    Four Hundred Monitor Calendar:  Latest info on national conferences, local events, & Webinars

    IT Jungle Store Top Book Picks

    BACK IN STOCK: Easy Steps to Internet Programming for System i: List Price, $49.95

    The iSeries Express Web Implementer's Guide: List Price, $49.95
    The iSeries Pocket Database Guide: List Price, $59
    The iSeries Pocket SQL Guide: List Price, $59
    The iSeries Pocket WebFacing Primer: List Price, $39
    Migrating to WebSphere Express for iSeries: List Price, $49
    Getting Started with WebSphere Express for iSeries: List Price, $49
    The All-Everything Operating System: List Price, $35
    The Best Joomla! Tutorial Ever!: List Price, $19.95

    RTFM Being Replaced by GTFM IBM Gooses Power Systems Storage and Networking

    Leave a Reply Cancel reply

Volume 11, Number 31 -- October 19, 2011
THIS ISSUE SPONSORED BY:

SEQUEL Software
Bug Busters Software Engineering
Botz & Associates, Inc.

Table of Contents

  • Return a User Profile Listing to a .NET Client
  • Dealing with Faulty Logic
  • Admin Alert: Adding Redundancy to Power i SMS Monitoring

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