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:41

I sometimes use goto when I want to perform a termination action:

static void DoAction(params int[] args)
{
  foreach (int arg in args)
  {
    Console.WriteLine(arg);
    if (arg == 93) goto exit;
  }

  //edit:
  if (args.Length > 3) goto exit;
  //Do another gazillion actions you might wanna skip.

  //etc.
  //etc.

exit:
  Console.Write("Delete resource or whatever");
}

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.

查看更多
我命由我不由天
3楼-- · 2019-04-18 14:41

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.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-04-18 14:43

I think the following excerpt from the Wikipedia Article on Goto is particularly relevant here:

Probably the most famous criticism of GOTO is a 1968 letter by Edsger Dijkstra called Go To Statement Considered Harmful. In that letter Dijkstra argued that unrestricted GOTO statements should be abolished from higher-level languages because they complicated the task of analyzing and verifying the correctness of programs (particularly those involving loops). An alternative viewpoint is presented in Donald Knuth's Structured Programming with go to Statements which analyzes many common programming tasks and finds that in some of them GOTO is the optimal language construct to use.

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 the GOTO 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.

查看更多
够拽才男人
5楼-- · 2019-04-18 14:43

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.

查看更多
孤傲高冷的网名
6楼-- · 2019-04-18 14:45

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.

查看更多
【Aperson】
7楼-- · 2019-04-18 14:47

"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

查看更多
登录 后发表回答