GOTO lazy programming?

I did have one teacher in college give one example of where a GOTO wasn’t completely awful… as a test in the beginning of a loop. The logic was something like this:


DO WHILE (forever condition)
   (some housekeeping);
   IF (some exit condition) THEN GO TO labelA;
   (the rest of the loop)
END DO;
labelA:

Why he suggested that, vs. a simple DO WHILE (exit condition is false), I honestly don’t recall… it made sense at the time.

More modern languages (this was PL/1) have more explicit structures such as Java’s break or continue commands.

The most egregious use of GOTO I saw was when maintaining some old code at my first job.


GOTO labelA
....
....
IF somecondition THEN
     statement
     statement
     GOTO labelB  /* note that this is an unconditional statement, nothing below it in the IF will EVER execute */
     statement
     statement
     labelA:
     statement
     statement
END IF;
statement
statement
statement
labelB:
statement

Yes - there are statements in the middle of the IF that would never get executed… except that some of them would get executed by branching INTO THE MIDDLE OF THE IF CLAUSE.

The ability to do shit like this is all it took to convince me that GOTOs were intrinsically STOOPID.

There is (almost) always a better way of doing things than to use a GOTO.

In 13 years of C programming, I’ve never used a GOTO, and if I find myself even considering using one (or a continue, or any one of a number of other suspect behaviours), the it’s a sign that I need to take a step back and rethink what I’m trying to do. Typically, a moment’s thought reveals a simpler, more elegant solution than the one I was about to blunder into.

If I see a GOTO in code I’m maintaining, then I make judgements about that programmer, and I won’t be terribly surprised if the code turns out to be not particularly well thought out, poorly commented and confusing.