Go To Statement Considered Harmful?

2019-04-18 14:19发布

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?

标签: c# .net goto bcl
15条回答
爷的心禁止访问
2楼-- · 2019-04-18 14:34

what about a double loop or many nested loops, of which you have break out, for ex.

 foreach (KeyValuePair<DateTime, ChangedValues> changedValForDate in _changedValForDates)
            {
                foreach (KeyValuePair<string, int> TypVal in changedValForDate.Value.TypeVales)
                {
                    RefreshProgress("Daten werden geändert...", count++, false);

                    if (IsProgressCanceled)
                    {
                        goto TheEnd; //I like goto :)
                    }
                }
            }
TheEnd:

in this case you should consider that here the following should be done with break:

  foreach(KeyValuePair<DateTime, ChangedValues> changedValForDate in _changedValForDates)
    {
                    foreach (KeyValuePair<string, int> TypVal in changedValForDate.Value.TypeVales)
                    {
                        RefreshProgress("Daten werden geändert...", count++, false);

                        if (IsProgressCanceled)
                        {
                            break; //I miss goto :|
                        }
                    }

                 if (IsProgressCanceled)
                 {
                            break; //I really miss goto now :|
                 }//waaaAA !! so many brakets, I hate'm
    }
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-04-18 14:35

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.

查看更多
我只想做你的唯一
4楼-- · 2019-04-18 14:36

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 a goto in the original code. There might be a more structured command to control the flow, like a break or a continue which has been implemented by the compiler in the same way as a goto, so that Reflector can't tell the difference.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-04-18 14:36

GOTO can be useful, if it's not overused as stated above. Microsoft even uses it in several instances within the .NET Framework itself.

查看更多
可以哭但决不认输i
6楼-- · 2019-04-18 14:38

goto considered harmful (for human to use but for computers its okay).

because no matter how madly we(human) use goto, compiler always knows how to read the code.

Believe me...

Reading others code with gotos in it is HARD.
Reading your own code with gotos 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...) ;)

查看更多
▲ chillily
7楼-- · 2019-04-18 14:40

In decompiled code, virtually all gotos 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.

查看更多
登录 后发表回答