Go Down

Topic: Whats up with GOTO command? (Read 9 times) previous topic - next topic

AznCaleb

Thanks pwillard I didn't know much about how to use the "hammer" or why the hammer was better than the "rock" but I'm getting the point about functions over GOTO's now.

Quote:
The question should not be "What's up with it?" but rather "is it the best choice?" when using it in your code.
I should have restated the topic to "Is it the best choice" because that was kind of what I was asking, I just phrased it weird sorry  ;)

george_graves

goto == the crazy aunt you don't talk about  8-)


Ray Andrews

Quote
goto == the crazy aunt you don't talk about


;D  Thats the most succinctly I've ever seen it put.

crimony

#23
May 12, 2009, 03:37 am Last Edit: May 12, 2009, 03:38 am by crimony Reason: 1
Unconditional jumps are used extensively in C, "break" and "continue" are both unconditional jumps - "goto" is simply an unconditional jump to a label.

goto's implemented over short distances to clear labels are justified when you need to balance speed, minimisation of stack use and minimisation of code size.

The most useful situation I have seen goto's used is in the context of a device driver, when a sequence of resources need to be secured prior to an operation being performed. In the event of an error associated with the allocation of one of the resources, the resources already allocated need to be released in the opposite order to their allocation (it's good practice to do this to avoid deadlock). goto offers a concise solution to this operational paradigm:

Code: [Select]

get resource A
if (error)
 goto out_a;
get resource B
if (error)
 goto out_b;
get resource C
if (error)
 goto out_c;
perform actions;
goto out;
out_c:
 undo C
out_b:
 undo B:
out_a:
 undo A
out:
 return ret;


The alternative, using nested if's, looks worse and requires a lot of extra work if you add another resource to allocate, or if you use functions then you use up limited stack space (unless you inline, and then you get code bloat)

Frankly, if you can follow program flow which uses break or continue (which don't have labels), you should be able to follow program flow which uses goto sparingly and clearly. If you can't, then you have no business touching someone else's device driver code.

Overall, I think that the education strategy of goto avoidance is worthwhile, but at some point you ought to become proficient enough to be trusted with all of a language's features, judiciously applied.


AlphaBeta

I think this is better/more concise.
Although slower, I would presume that the speed of performAction is a lot more important the than the preparation for it.
Code: [Select]

if (getResourceA() && getResourceB() && getResourceC()){
 performActions();
} else {
 releaseResourceC();
 releaseResourceB();
 releaseResourceA();
}


I do not see 'break' or 'continue' as unconditional jumps. You are aware of exactly what your code will do, and how it effects program flow, if using there keywords.
A goto out_a; might not be as clear, and thus not comparable. (That's ofcource my opinion.)

All this said, I agree with:
Quote
Overall, I think that the education strategy of goto avoidance is worthwhile, but at some point you ought to become proficient enough to be trusted with all of a language's features, judiciously applied.


:)

Go Up