tags:

views:

140

answers:

3

Possible Duplicate:
Examples of good gotos in C or C++

What uses of the goto statement do you consider acceptable? I am discussing some coding guidelines for C embedded work, and wondering if there are cases where goto is the cleanest way to do things.

A: 

For C, I find it useful to use goto for a common exit point in some functions. If you need to release resources before returning, it's nice to do retval = -ERROR; goto fn_exit; instead of trying to break out of multiple layers of for/while loops.

Some would argue that it isn't necessary in well designed code -- when you reach a point where using goto is attractive, you should be breaking the function up into multiple sub-functions. Maybe that's true in some cases, but if you have to pass multiple variables in, or pass pointers to variables so a sub-function can update those values, I feel like you've added unnecessary complications.

Here's a recent SO question on using goto for error management. I'm sure that browsing the goto tag will get you even more answers.

tomlogic
A: 

I found that there are quite a few places where goto might simplify the logic. My rule is that no 2 goto's overlap each other and that the goto and its label are 'close'; usually, no more than 30 lines apart.

Usually, when I have a complex condition that I have to check many things, a goto might help:

if ( condition1 && cond2 )
  if ( checkFile( file ) )
    goto DONE;
else
  if ( condition3 )
    goto DONE;

handleError();
return 0;

DONE:
...do something...
return 1;
Gianni
I can think of more elegant solutions *without* goto, so this is not a strong argument. For example creating a function `int done()`, and replacing `goto DONE` with `return done()`. There is almost always a better way than to resort to goto.
Clifford
@Clifford you are right, my example above is very simplistic; but that is the point: the use of goto I propose is for complex logic, which is not easily demonstrated by a simple example. Now, in some complex problems, I've found that a simple goto is more direct and clear as to what the intention is better than other solutions.
Gianni
@Clifford are you proposing multiple return statements within a given function?
simon
@simon: No, I merely adapted Gianni's structure into the most obvious solution without goto. Multiple returns is a separate argument, and I accept only marginally better. The comment box does not lend itself to describing a better solution, but since the example is contrived, I chose to ignore that point.
Clifford
@Gianni: Perhaps you have over complicated your example in order to contrive an example, but if it is an example of the kind of "complex logic" that leads to acceptable use of goto, I would suggest that the first resort should be to reduce the complexity. I would call this 'convoluted' not 'complex'
Clifford
@Clifford: I actually fond a piece of my own code where I use goto. Then tried to reduce it to a meaningful example. Complex logic is a fact of life, our job is to handle this complexity, and I find that sometimes goto is a good tool for the job. Just because goto can be used to create the worst possible code, does not mean that evey use will be bad.
Gianni
+1  A: 

My personal opinion is that there are NO acceptable uses of the goto statement in a modern programming language.

"GOTO Statement Considered Harmful", by the late Edsger W. Dijkstra, does a good job of covering the issue. That paper should be required reading for every software developer on the planet.

It is worth noting that the Gypsy language, from Don Good's group at UT Austin in the late 1970s, did not have a goto.

It is worth noting that Ichbiah et al only included a goto in Ada because the DoD required it, explicitly, in so many words in the requirements specification. I remember reading that Ichbiah and his team deliberately made the goto target label syntax as ugly as they could, to make the labels stick out like sore thumbs, and discourage use of the goto.

John R. Strohm