Control Structures
GO TO is both widely used and widely deplored. Here we will merely
summarize a broader debate:
- Don't use GO TO if you don't have to.
- You (almost) never have to.
If you don't use GO TO this rule mostly takes care of itself, except
when fall-through logic starts at the initial entry point of a module.
Don't use the SORT verb.
Among other reasons, this rule eliminates one possible reason to use GO TO.
If you don't use GO TO or fall-through logic, a THRU clause on a PERFORM
statement is a useless bug-breeder.
Don't use sections in the PROCEDURE
DIVISION.
Except when the compiler requires them, the use of sections invites
problems without solving any.
You may have to use segmentation to get a large program to fit
into a small machine. However, careless use of segmentation invites
bizarre and mysterious bugs.
Use CONTINUE instead of NEXT
SENTENCE.
These two constructs are not quite the same, and CONTINUE is safer.
Always include a default clause in an case structure.
If you code an EVALUATE statement, include a WHEN OTHER clause. If
you use a series of ELSE IF clauses instead, include a final ELSE
clause.
If you follow this rule, you will at least think about what
to do in the default case. If there is nothing to do, then code a
CONTINUE statement, just to show that you did think about it.
It does not follow that you should code an ELSE for every IF,
as sometimes recommended. All those pointless CONTINUEs (or NEXT
SENTENCEs) are an annoyance to type and a distraction to read.
For short loops, use an in-line PERFORM.
Before COBOL II, a PERFORM UNTIL required a separate paragraph, even if
it merely decremented a counter. An in-line PERFORM can be more
readable because you don't have to look somewhere else to see what the
loop is doing.
Don't overdo it. Most non-trivial loops -- more than about five lines
-- deserve paragraphs of their own. Otherwise the PERFORMing paragraph
may become too big and complicated. Main exception: when the loop
makes up almost the entire paragraph anyway.
Use 88-levels whenever possible for flags and
switches.
In most cases the name of a flag or switch variable shouldn't even
appear in the PROCEDURE DIVISION. Test it with the 88-levels, and
use the SET verb to assign a value to it.
Some people argue that you should never code a period as a statement
delimiter, except at the end of a paragraph, where the compiler
requires one. Instead, delimit every IF with END-IF, every READ with
END-READ, and so forth.
COBOL Home
COBOL Style Forum