In Defence of GOTO

Posted by ludamad on Nov. 21, 2008, 4:48 p.m.

Recently I've been reading up a lot on Computer Science, mostly on the topic of algorithms and computational complexity, and one oft repeated statement is "Don't use GOTO or you'll be mauled by raptors", or something to that effect. I feel that this is a great exaggeration - the raptors proved quite friendly.

For those who don't know what GOTO is, basically its a low-level (machine code) statement that makes a program continue from the point it describes.

Consider:

a = 0;

DoItAgain: //The GOTO label

a+=1;

if (a<5)

goto DoItAgain; //The GOTO statement

Here of course, one would use either a for or while loop if they wanted something like this. And, though that is undoubtedly the best thing to do in this case, realize that every loop is essentially built off of GOTO. Without GOTO at assembly level, programming wouldn't be possible.

The question is, then, is GOTO appropriate for high level programming? I believe emphatically that it is. If we could simply use a loop wherever a GOTO would be useful, the matter would be different. But GOTOs allow us to cut corners, and to divert program flow in very complex ways in very clear code. Yes, I said 'GOTO' and 'clear' in the same sentence.

This is one of the most common reasons one must embrace GOTO - escaping multiple nested loops of death. The break command will only ever leave one loop, meaning you will have to keep a flag telling the other loops to terminate. More elegant is:

while (something)

while (something)

while (something)

{

if (I_want_to_get_out)

goto Out;

}

Out:

//more code

GOTO is often criticized for not letting code be read in a linear way by anyone reading it, because it could potentially hop all around the place. Potentially hopping around the place and actually hopping around the place are different. In truth, most GOTO usages keep with linear reading of code. The above code had the GOTO label within view of the GOTO statement - very readable.

Additionally, using GOTO can lead to many optimizations in critical parts of code that a compiler might not pick up on, such as avoiding additional variables to keep track of things that a GOTO can easily avoid. Overall it is in my firm belief that we should not be too hasty in abandoning GOTO in high level programming, instead taking the route of making GOTOs more readable but essentially the same statement. C++ makes a good step towards this by allowing the same GOTO label names in multiple functions, although there is a general need to make label names for each and every GOTO that still makes the process less refined than other high level constructs at the moment.

Comments

Kenon 16 years ago

I was about to smack you in the face if you were going to do something like a GOTO loop, but yeah, you are right, they are useful in certain circumstances.

elmernite 16 years ago

Very nice, I hope to see some people's arguments against it. I'm just getting into full scale (Not GM) coding. I had heard that using the GOTO was like selling you soul to the stock broker. But I came across a case where the goto would have been kind of handy. I agree that perhaps it has a place in modern coding.

You have a nice little write out here on it, much better than I could have done.

-Elmernite

Rusky 16 years ago

Multiple loops is… let's see… one. One example. Woo, use goto now.

Okay, yes, optimization. But how often do you actually need that detailed of optimization? When you're writing a device driver for a nuclear power plant, maybe.

Only use goto sparingly, when it's nearby (like you mentioned) and if the alternative is ugly.

Now in assembly, jmp is a different matter, try writing anything without it :P

Rusky 16 years ago

(complicated I mean. like a device driver or kernel)

ludamad 16 years ago

If a piece of code is meant to be run millions of times, like perhaps a hash function vital to a system, the hate for GOTO should be forgotten very quickly if one hopes to optimize (eg, by saving overhead caused by flags). Readability can be given with comments; code is meant for computers to understand.

I am not writing "A Recommendation to Use GOTO Everywhere", simply an appeal against orthodoxy.

flashback 16 years ago

YOU BRING LIZARDY APOCALYPSE ON US ALL! FOOL!

JoshDreamland 16 years ago

Though defending goto; will only further sink my reputation as a poor coder, yeah, I agree.

That break; example especially, since break(2); never occurred to C++ people.

PY 16 years ago

GOTO Pain

ludamad 16 years ago

PY, the standard joke is:

goto Hell;

PY 16 years ago

Sorry, I am not versed in these C++ jokes.

NERDS.