Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
User Journal

Journal SurturZ's Journal: In defense of GOTO

I'm amazed at how religious some programmers are about not using the GOTO statement.

I was reading this (quite interesting, actually) article and noticed the author claim the following:

Once you realize this, you also realize that all those objections to the banning of the goto go away--there really is no place for it in well-written code. This may sound like an extreme viewpoint, but speaking as an experienced programmer, the only times I can recall needing a goto in the last 20 years were when using languages that didn't support adequate control constructs (most of which, thankfully, are extinct now). I have never written a goto in C code.

The author later details a piece of code that includes the C equivalent of a

Do
    If Something Then Exit Do
    If SomethingElse Then Exit Do
    etc ...
Loop While False

and then says this about it:

The second interesting feature of the above example routine is the do-once construct. This conforms to the rule for structures which nest: you enter it at the top, and exit it at the bottom. It allows for a linear sequence of any number of steps, any of which can fail, in which all prior steps must have succeeded in order for a given step to be processed.

Oddly, some people who see this construct for the first time tend to react unfavourably. It is a loop construct which is not being used for looping, and a few people don't like that. What can I say? It is a deficiency of the C language that it doesn't offer a construct specifically for this purpose, so reusing the do-construct (and carefully marking it as such right from the beginning, with the /*once*/ comment) is the best I can do. I need a structure for this purpose, and the alternatives that have been suggested to me are even worse. Deal with it!

I hate to break it to you dude, but there IS a structure, and it is called GOTO. A couple of line labels and you would be golden:

SeriesOfChecks:
    If Something Then GOTO SeriesOfChecksExit
    If SomethingElse Then GOTO SeriesOfChecksExit
    etc...
SeriesOfChecksExit:

I believe there is still a place for GOTO in modern programming. The main case where GOTO is useful is a situation as follows:

  • You have a transaction consisting of multiple steps
  • If any step fails, the transaction as a whole fails, and you do not need to perform the subsequent steps
  • Whether or not the transaction fails, some cleanup operations are required

A common example is parsing a file that might be corrupt or in the wrong format.

My belief is that GOTO is a fundamental building block of a programming language. You can replace a lot of structures (e.g. Sub, Function, Do...Loop) with GOTO, but you can't do the reverse.

Sparingly and properly used, GOTO can simplify code and improve readability.

This discussion has been archived. No new comments can be posted.

In defense of GOTO

Comments Filter:

Saliva causes cancer, but only if swallowed in small amounts over a long period of time. -- George Carlin

Working...