MainframeSupports
tip week 9/2010:

In this week I will cross the subject of programming style which typically leads to endless discussions in any IT department. Therefore some of you might think that this tip is not a tip, but a very bad idea.

Occasionally you have to do a series of validations in your COBOL programs. Perhaps you isolate those validations in a paragraph (the closest thing to a procedure or subroutine in COBOL which all other programming languages are equipped with except assembler). At the first failing validation you display an error message and leaves the paragraph. Now we are ready to embark on the subject of programming style. Leaving the paragraph may be coded in many ways. The usual and pragmatic solution is to code a GO TO to an exit paragraph following the validation paragraph.

If you hate GO TO just as much as I it becomes hard to code the validation paragraph in an orderly and understandable way. If COBOL was equipped with a RETURN statement just like PL/I or REXX...

01  returnArea pic x(1) value 'Y'.
  88  returnNow value 'Y'.
...
validations.
    if a = spaces
      display 'value for a missing'
      if returnNow next sentence end-if
    end-if
    if b not numeric
      display 'b must be numeric'
      if returnNow next sentence end-if
    end-if
    if (c < 'A' or c > 'F') and c not numeric
      display 'c must be hexadecimal'
      if returnNow next sentence end-if
    end-if
    .

NEXT SENTENCE in IBM COBOL makes progam execution continue after the next dot. Furthermore COBOL requires that NEXT SENTENCE is the only sentence in an IF or ELSE. You cannot get away with just writing NEXT SENTENCE anywhere to continue after the next dot. That is why I have invented the somehow peculiar statement IF returnNow NEXT SENTENCE END-IF. Luckily it quite easily fits on one line. And why not just write IF 1 = 1 instead of a conditional variable. That is because you have to, because COBOL does not allow conditions which are obvious either true or false.

You may disagree on the beauty and readability of the above example, but it works as designed. Please be aware of the very important detail that NEXT SENTENCE resumes execution after the next dot. If the next dot closes the paragraph execution resumes at the statement following the perform which called the paragraph. In COBOL I will advice you only to use dots where they are required and only to use paragraphs in procedure division.

Previous tip in english        Sidste danske tip        Tip list