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?
what about a double loop or many nested loops, of which you have break out, for ex.
in this case you should consider that here the following should be done with break:
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.In response to your edit:
No, not all gotos are compiler generated, but a lot of them result from compiler generated state machines (enumerators), switch case statements or optimized if else structures. There are only a few instances you'll be able to judge whether it was the compiler or the original developer. You can get a good hint by looking at the function/class name, a compiler will generate "forbidden" names to avoid name clashes with your code. If everything looks normal and the code has not been optimized or obfuscated the use of goto is probably intended.
The general rule is that you don't need to use
goto
. As with any rule there are of course exceptions, but as with any exceptions they are few.The
goto
command is like a drug. If it's used in limited amounts only in special situations, it's good. If you use too much all the time, it will ruin your life.When you are looing at the code using Reflector, you are not seeing the actual code. You are seeing code that is recreated from what the compiler produced from the original code. When you see a
goto
in the recreated code, it's not certain that there was agoto
in the original code. There might be a more structured command to control the flow, like abreak
or acontinue
which has been implemented by the compiler in the same way as agoto
, so that Reflector can't tell the difference.GOTO can be useful, if it's not overused as stated above. Microsoft even uses it in several instances within the .NET Framework itself.
because no matter how madly we(human) use
goto
, compiler always knows how to read the code.Believe me...
Reading others code with
goto
s in it is HARD.Reading your own code with
goto
s in it is HARDER.That is why you see it used in low level (machine languages) and not in high level (human languages e.g. C#,Python...) ;)
In decompiled code, virtually all
goto
s that you see will be synthetic. Don't worry about them; they're an artifact of how the code is represented at the low level.As to valid reasons for putting them in your own code? The main one I can think of is where the language you are using does not provide a control construct suitable for the problem you are tackling; languages which make it easy to make custom control flow systems typically don't have
goto
at all. It's also always possible to avoid using them at all, but rearranging arbitrarily complex code into a while loop and lots of conditionals with a whole battery of control variables... that can actually make the code even more obscure (and slower too; compilers usually aren't smart enough to pick apart such complexity). The main goal of programming should be to produce a description of a program that is both clear to the computer and to the people reading it.