Adding New Paragraphs

A Simple Example

Consider the following paragraph, which you invoke with a PERFORM THRU. You added the second GO TO in an earlier stage in order to eliminate fall-through logic.
 2000-PROCESS-FILE.
*
     PERFORM 2900-READ-FILE.
*
     IF EOF
        GO TO 2000-EXIT.
*
*    lots of complicated logic here, but no GO TOs
* 
     GO TO 2000-EXIT.
*
 2000-EXIT.
     EXIT.
At an earlier stage, while you were eliminating trivial GO TOs, you left this paragraph alone. The complicated logic kept the GO TO from being trivial.

Now that you have eliminated fall-through logic, it is safe to pull out the complicated logic into a new paragraph:

 2000-PROCESS-FILE.
*
     PERFORM 2900-READ-FILE.
*
     IF EOF
        GO TO 2000-EXIT.
*
     PERFORM 2010-PROCESS-RECORD.
     GO TO 2000-EXIT.
*
 2000-EXIT.
     EXIT.
*
 2010-PROCESS-RECORD.
*
*    lots of complicated logic here, but no GO TOs
* 
Clearly this change does not alter the logic, but it renders the GO TO trivial. With a few more keystrokes you can eliminate it:
 2000-PROCESS-FILE.
*
     PERFORM 2900-READ-FILE.
*
     IF EOF
        CONTINUE
     ELSE
        PERFORM 2010-PROCESS-RECORD.
*
     GO TO 2000-EXIT.
*
 2000-EXIT.
     EXIT.
*
 2010-PROCESS-RECORD.
*
*    lots of complicated logic here, but no GO TOs
* 
Now you are left with a trivial EXIT paragraph, which you can eliminate (along with the corresponding THRU clause somewhere else, and the GO TO at the end):
 2000-PROCESS-FILE.
*
     PERFORM 2900-READ-FILE.
*
     IF NOT EOF
        PERFORM 2010-PROCESS-RECORD.
*
 2010-PROCESS-RECORD.
*
*    lots of complicated logic here, but no GO TOs
* 
You have eliminated two GO TOs and a THRU clause. Because you did it in a series of small steps, each of which was was clearly valid, you can be confident that you preserved the original logic.

A Less Simple Example: Multiple GO TOs

In the preceding example, it is important that the complicated logic contained no GO TOs. Otherwise you couldn't have pulled it into a separate paragraph.

Let's make the example a bit more complicated:

 2000-PROCESS-FILE.
*
     PERFORM 2900-READ-FILE.
*
     IF EOF
        GO TO 2000-EXIT.
*
     PERFORM 2100-VALIDATE-RECORD.
*
     IF NOT VALID-RECORD
	GO TO 2000-EXIT.
*
*    lots of complicated logic here, but no GO TOs
* 
     GO TO 2000-EXIT.
*
 2000-EXIT.
     EXIT.
You can apply the same process to eliminate the GO TOs in stages, from the bottom up (not counting the GO TO at the very end). After the first stage the code looks like this:
 2000-PROCESS-FILE.
*
     PERFORM 2900-READ-FILE.
*
     IF EOF
        GO TO 2000-EXIT.
*
     PERFORM 2100-VALIDATE-RECORD.
*
     IF VALID-RECORD
	PERFORM 2020-PROCESS-VALID-RECORD.
*
     GO TO 2000-EXIT.
*
 2000-EXIT.
     EXIT.
*
 2020-PROCESS-VALID-RECORD.
*
*    lots of complicated logic here, but no GO TOs
*
(For brevity I omit the intermediate steps, and the comment blocks that should preface every paragraph.) Now the code looks a lot like the first example, and you can transform it further in the same way:
 2000-PROCESS-FILE.
*
     PERFORM 2900-READ-FILE.
*
     IF NOT EOF
        PERFORM 2010-PROCESS-RECORD.
*
 2010-PROCESS-RECORD.
*
     PERFORM 2100-VALIDATE-RECORD.
*
     IF VALID-RECORD
        PERFORM 2020-PROCESS-VALID-RECORD.
*
 2020-PROCESS-VALID-RECORD.
*
*    lots of complicated logic here, but no GO TOs
*

More Complications

A paragraph may have multiple GO TOs, some under various nested IF clauses, some leading to other destinations.

Don't try to untangle it all at once. If you can find a clear and simple way to isolate some of the logic in a separate paragraph, you may be able to apply a simple transformation to what remains. If you can't, don't worry about it. Later stages of transformation will create new opportunities.

Naming the New Paragraphs

The resulting new paragraphs are wholly artificial byproducts of the rewrite process. If they turn out to be well structured, cohesive modules, great. Just be sure to give them good names.

However, they are likely to be ugly hodge-podges. You may not be able to concoct accurate paragraph names within 30 characters. You may not even be sure what the paragraphs do. If so, give them names as as artificial as the paragraphs themselves -- something like 2020-EXTRA-PARA. Better to have a meaningless name than a misleading one.


[style forum]COBOL Style Forum [spaghetti]Spaghetti code [stage]Next stage
[home]COBOL Home