Against GO TO

GO TO in other languages

The following summary is mostly based on the discussion by Steve McConnell in Code Complete: These points apply to COBOL as well as other languages. In COBOL, GO TO creates more problems, as outlined below.

GO TO in COBOL

McConnell didn't write about COBOL. He wrote mostly about languages like C, where a goto has limited scope. A goto in C can branch only to a destination within the same function where the goto resides. Even if that one function is a tangled mess, the tangle does not extend beyond the bounds of the function.

In COBOL, a GO TO is not bounded in this way. You can GO TO any paragraph within the same section, or to any section in the program. If you don't use sections, you can GO TO any paragraph in the same program. Once you GO TO, you're stuck with fall-through logic until you hit an EXIT, GOBACK, or similar construct.

These possibilities undermine modularity. You can't tell what happens at the end of a paragraph unless you know how you entered the top. When used excessively, GO TOs can make a program so convoluted that you can't understand any of it without understanding all of it.

Reading a Program with GO TOs

When I study an unfamiliar program, I first scan for all the GO TOs. If there aren't any, I heave a sigh of relief. Otherwise I have to examine every single one of them to determine: This analysis is tedious, time-consuming, and error-prone, especially if there are multiple possible paths before or after the GO TO. Until I complete this analysis, however, I can't safely change any of the logic, because I won't know the impact of any change I make.

Some of these difficulties arise not from GO TO but from PERFORM THRU and the associated fall-through logic. In the absence of GO TO, however, it is trivial (though tedious) to eliminate all fall-through logic.

Disciplined GO TO's

Care and discipline in the use of GO TO can reduce these problems. In effect, one can use GO TO to implement the standard constructs of structured programming. As a result, the scope of each GO TO is both limited and easily identified.

While such discipline may reduce the analysis required to understand a program, it does not eliminate it. You still have to identify the scope of every GO TO and every PERFORM THRU.

Without GO TO or PERFORM THRU, the analysis is greatly streamlined. You can readily identify the contexts in which a paragraph is PERFORMed. You can add paragraphs, break them into pieces, or move them around, without fear that you will unexpectedly disrupt some stream of fall-through logic.


homeCOBOL Home [style forum]COBOL Style Forum