Coping with Sections

COBOL sections add a layer of aggregation between paragraphs on the one hand and the entire PROCEDURE DIVISION on the other. The syntax for sections is similar to the syntax for paragraphs, but different enough to create problems.

When getting rid of GO TOs through a systematic series of transformations, begin by eliminating the use of sections in the PROCEDURE DIVISION.

(In some cases, as in the use of declaratives, you cannot avoid the use of sections. In that case your best hope is to rewrite each section separately as needed.)

Eliminate sections step by step

The following procedure will eliminate most sections most of the time, but some programs may require ingenuity on a case-by-case basis.

1. Add a trivial EXIT paragraph to the end of each section

Some sections may already have EXIT paragraphs at the end. Leave them there.

Make sure that each EXIT paragraph has a unique name within the entire PROCEDURE DIVISION (not just within the section). If a section already has an EXIT paragraph, you may need to rename it.

The new EXIT paragraphs are safe because they do not affect any existing flow of control. When not the target of an active THRU clause, the EXIT paragraph behaves like a CONTINUE statement.

2. Transform existing PERFORM THRUs of sections

If a PERFORM THRU invokes a series of adjacent sections, change it to a series of separate PERFORM statements, one for each section. For example, suppose the original code says PERFORM A THRU C, and B is a section in the middle. You'd like it to read:
     PERFORM A.
     PERFORM B.
     PERFORM C.
In the presence of GO TO, however, this transformation may not be safe. Suppose A contains GO TO C, thereby bypassing B.

Another possible problem is a PERFORM THRU where the target of the THRU clause is somewhere in the middle of a section. Perhaps you can rearrange the paragraphs and direct the THRU clause to the EXIT paragraph. However, you must be aware of the possibility that the section will receive control, not only through a PERFORM, but also by a GO TO, or by a fall-through from the preceding section. In that case the flow of control is different.

Such contortions may be difficult to unravel. Proceed with caution.

One way or another, your goal at this point is to ensure that:

  1. You don't PERFORM THRU more than a single section;
  2. Control always leaves a PERFORMed section from the end of that section.

3. Change all PERFORMs of single sections into PERFORM THRUs

The target of the THRU clause should be the EXIT paragraph at the end of the section. This change is safe because it does not affect the flow of control.

When you PERFORM a section without using a THRU clause, control returns to the PERFORMing code when it reaches the next SECTION header.

With a PERFORM THRU, control returns when it has executed the target of the THRU clause. Since you have put that target at the end of the section, the results are the same as if there were no THRU clause.

4. Remove the SECTION keyword from the section headers

Provided that you have completed all the preceding steps successfully, this change is safe because it does not disturb the existing flow of control.

You may need to rename some paragraphs so that the names will be unique within the entire PROCEDURE DIVISION, not just within a section.

Conclusion

It is probably impossible to briefly specify a procedure which will always get rid of sections correctly. There are too many ways for the flow of control to become tangled.

Consequently, you should not follow these guidelines blindly. Be aware of the more perverse possibilities, if only so that you can rule them out. If you find a particularly twisted bit of logic, you'll just have to wrestle with it by any means necessary until it takes a more familiar form.


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