In Cobol, to test "null or empty" we use "NOT = SPACE [ AND/OR ] LOW-VALUE" ? Which is it?

Tom picture Tom · Nov 4, 2010 · Viewed 84.9k times · Source

I am now working in mainframe, in some modules, to test

Not null or Empty

we see : NOT = SPACE OR LOW-VALUE The chief says that we should do : NOT = SPACE AND LOW-VALUE

Which one is it ?

Thanks!

Answer

NealB picture NealB · Nov 4, 2010

Chief is correct.

COBOL is supposed to read something like natural language (this turns out to be just another bad joke).

Lets play with the following variables and values:

 A = 1
 B = 2
 C = 3

An expression such as:

IF A NOT EQUAL B THEN...

Is fairly straight forward to understand. One is not equal to two so we will do whatever follows the THEN. However,

IF A NOT EQUAL B AND A NOT EQUAL C THEN...

Is a whole lot harder to follow. Again one is not equal to two AND one is not equal to three so we will do whatever follows the 'THEN'.

COBOL has a short hand construct that IMHO should never be used. It confuses just about everyone (including me from time to time). Short hand expressions let you reduce the above to:

IF A NOT EQUAL B AND C THEN...

or if you would like to apply De Morgans rule:

IF NOT (A EQUAL B OR C) THEN...  

My advice to you is avoid NOT in exprssions and NEVER use COBOL short hand expressions.

What you really want is:

 IF X = SPACE OR X = LOW-VALUE THEN...
    CONTINUE
 ELSE
    do whatever...
 END-IF

The above does nothing when the 'X' contains either spaces or low-values (nulls). It is exactly the same as:

 IF NOT (X = SPACE OR X = LOW-VALUE) THEN
    do whatever...
 END-IF

Which can be transformed into:

 IF X NOT = SPACE AND X NOT = LOW-VALUE THEN...

And finally...

 IF X NOT = SPACE AND LOW-VALUE THEN...

My advice is to stick to simple to understand longer and straight forward expressions in COBOL, forget the short hand crap.