If the statement above is correct, then why when I use reflector on .Net BCL I see it is used a lot?
EDIT: let me rephrase: are all the GO-TO's I see in reflector written by humans or compilers?
If the statement above is correct, then why when I use reflector on .Net BCL I see it is used a lot?
EDIT: let me rephrase: are all the GO-TO's I see in reflector written by humans or compilers?
I sometimes use goto when I want to perform a termination action:
So instead of hitting
return
, I send it to the last line that performs another final action I can refer to from various places in the snippet instead of just terminating.These goto's are very often generated by the compiler, especially inside enumerators. The compiler always knows what she's doing.
If you find yourself in the need to use goto, you should make sure it is the only option. Most often you'll find there's a better solution.
Other than that, there are very few instances the use of goto can be justified, such as when using nested loops. Again, there are other options in this case still. You could break out the inner loop in a function and use a return statement instead. You need to look closely if the additional method call is really too costly.
I think the following excerpt from the Wikipedia Article on Goto is particularly relevant here:
So, on the one hand we have Edsger Dijkstra (a incredibly talented computer scientist) arguing against the use of the
GOTO
statement, and specifically arguing against the excessive use of theGOTO
statement on the grounds that it is a much less structured way of writing code.On the other hand, we have Donald Knuth (another incredibly talented computer scientist) arguing that using
GOTO
, especially using it judiciously can actually be the "best" and most optimal construct for a given piece of program code.Ultimately, IMHO, I believe both men are correct. Dijkstra is correct in that overuse of the
GOTO
statement certainly makes a piece of code less readable and less structured, and this is certainly true when viewing computer programming from a purely theoretical perspective.However, Knuth is also correct as, in the "real world", where one must take a pragmatic approach, the
GOTO
statement when used wisely can indeed be the best choice of language construct to use.When compiled down to assembly code, all control structured and converted to (un)conditional jumps. However, the optimizer may be too powerful, and when the disassembler cannot identify what control structure a jump pattern corresponds to, the always-correct statement, i.e.
goto label;
will be emitted.This has nothing to do with the harm(ful|less)ness of
goto
.Go To
statement itself is not harmful, it is even pretty useful sometimes. Harmful are users who tend to put it in inappropriate places in their code."C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book."
-- K&R (2nd Ed.) : Page 65