为什么 goto 实践不佳?
此问题与 Goto 仍被认为有害重复。 如果您想进一步讨论这个问题,请使用原来的问题。
为什么 GOTO 是糟糕的编程实践? 在某些场合使用它是有意义的。
This question is a duplicate of Goto Still Considered Harmful. If you wish to discuss this further please use the original question.
Why exactly is GOTO poor programming practise? It makes sense on some occasions to use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
请注意,Djkstra 写的是“GOTO 被认为是有害的”,而不是“GOTO 被认为是致命的”。 它有它的地方。 不幸的是,在您拥有一定的维护他人代码的经验之后,才能判断何时使用它,尤其是由不再在您公司工作的人几年前编写的代码。 因此,在您拥有这种经验之前,最好尽可能避免使用 goto。
Note that Djkstra wrote "GOTO Considered Harmful", not "GOTO Considered Deadly". It has its places. Unfortunately, the judgement to know when to use it comes after you've got some experience in maintaining other people's code, especially code that was written years ago by people who no longer work in your company. Thus, it's best to avoid goto as much as you can until you have that experience.
基本上,它鼓励了不良的编码风格。 请参阅:Goto 被认为有害 [pdf]
It encourages bad coding style, basically. See: Goto Considered Harmful [pdf]
它可以迅速导致意大利面条代码。
It can rapidly lead to spaghetti code.
这意味着您还没有按程序设计代码。
如果你正确地构建了 while 和 ifs,你应该很少需要 goto。
很少是关键词。 有时它很有用。 从这个意义上说,现在许多人只是显式地抛出和捕获异常,只是为了避免可怕的 goto。
It means you haven't designed the code procedurally.
If you build your whiles and ifs properly, you should rarely ever need goto.
Rarely is the key word. There are times where it's useful. In that sense, many people nowadays just explicitly throw and catch exceptions, just to avoid the dreaded goto.
因为代码的结构可能很难理解。
这
比
Because the structure of the code can be difficult to follow.
This
is much less clear than
GOTO 使您的控制流程短路,从而破坏您的设计并为调试和维护噩梦打开大门。
GOTO short circuits your control flow, thereby undermining your design and opening the door to debug and maintenance nightmares.
可读性成为一个问题,并且使用 goto 可能会产生意想不到的逻辑流。
我认为 .net 编译器将更改一些 case/switch 语句以使用 goto 是很有趣的。
Readability becomes a problem, and flow of logic can have unintended effects by the use of goto.
I think its funny that the .net compiler will change some case/switch statements to use goto.
在某些情况下这是有道理的。 但是,在大多数情况下,使用它被认为是不好的做法,因为与使用结构化代码(例如 for 循环和 while 循环)相比,它会使代码的执行路径难以阅读。
当您查看 goto 或标签时,如果不阅读或搜索代码来查找标签名称,您实际上并不知道它去向或来自哪里。 这是很棘手的,尤其是当 goto 与标签不适合在同一屏幕上时。 将其与 for 循环或 while 循环或 if.. 进行比较,您可以从结构(即代码的缩进)中看出执行路径是什么。
话虽如此,它有时很有用,特别是当跳出一些在多维数组内挖掘特定值的嵌套 for 循环时。 当找到正确的值时,您可以在此处使用 goto 跳出 for 循环。
In some cases it makes sense. But, in the majority of cases use of it it considered poor practice because it can make the execution path of code tricky to read compared to using structured code such as for loops and while loops.
When you look at a goto or a label, you don't really know where is goes to or comes from without reading or searching through the code for the label name. This is tricky especially if the goto doesn't fit on the same screen as the label. Compare this to a for loop or a while loop or an if.. you can tell from the structure (i.e. indenting of the code) what the execution path is.
Having said this, it sometimes is useful, especially for example when jumping out of some nested for loops that are digging for a particular value inside a multi-dimensional array. You could use a goto here to jump out of the for loops when the correct value is found.