PHP 中的 GOTO 是邪恶的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1900017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:13:52  来源:igfitidea点击:

Is GOTO in PHP evil?

phpgoto

提问by Tower

I recently found out that PHP 5.3 supports new language construct called GOTO. Everybody knows what it does. However, it's not exactly the traditional GOTO, it's just a jump label. I'm interesting in knowing whether this GOTOis evil and implies bad code?

我最近发现 PHP 5.3 支持名为GOTO. 每个人都知道它的作用。然而,它并不完全是传统的GOTO,它只是一个跳转标签。我很想知道这是否GOTO是邪恶的并暗示了错误的代码?

回答by Konamiman

Unless you are programming in assembler, GOTO should always be treated the same way as the life vest of the airplanes: it is good to have them available, but if you need to use them it means that you are in big trouble.

除非你是用汇编程序编程,否则应该始终像对待飞机的救生衣一样对待 GOTO:拥有它们固然好,但如果您需要使用它们,则意味着您遇到了大麻烦。

回答by Loris

I can't believe nobody posted this :)

我不敢相信没有人发布这个:)

xkcd - goto

xkcd - 转到

Granted, PHP is not compiled... Maybe the raptor will chase you on every visit to your website?

当然,PHP 没有被编译......也许猛禽会在每次访问您的网站时追逐您?

回答by Remo.D

Bad structuring of code is evil, regardless the control structure you use.

无论您使用哪种控制结构,错误的代码结构都是邪恶的。

I personally prefer a goto that makes clear the flow of the program to "control variables" and nested "if" that will indirectly just cause the same branch in the code.

我个人更喜欢 goto ,它可以明确程序“控制变量”和嵌套“if”的流程,这将间接导致代码中的相同分支。

So, just write the two versions (with and without GOTO) and see which one it's easier to comprehend. Then the choice is easy.

所以,只写两个版本(有和没有 GOTO),看看哪个更容易理解。那么选择就很简单了。

回答by schmunk

I think this is the most important part of the PHP manual pageand missing here:

我认为这是PHP 手册页中最重要的部分,这里缺少:

This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one. You also cannot jump into any sort of loop or switch structure. You may jump out of these, and a common use is to use a goto in place of a multi-level break.

不是一个完全不受限制的 goto。目标标签必须在同一个文件和上下文中,这意味着你不能跳出一个函数或方法,也不能跳进一个. 您也无法跳入任何类型的循环或切换结构。您可能会跳出这些,一个常见的用途是使用 goto 代替多级中断。

IMHO this makes it very different from the ye olde BASIC style gotos.

恕我直言,这使它与ye olde BASIC 风格的 goto非常不同。

回答by AdamJonR

I'm in the minority (currently), but I believe the restrictions placed on PHP's goto construct make a very beneficial tool:

我是少数(目前),但我相信对 PHP 的 goto 构造的限制是一个非常有用的工具:

http://adamjonrichardson.com/2012/02/06/long-live-the-goto-statement/

http://adamjonrichardson.com/2012/02/06/long-live-the-goto-statement/

I actually walk through an example of arrow code (deeply nested conditionals) and refactor it using standard practices (guard clauses, grouping conditions, pulling out functions) in one version and a goto-based version in the other version, and I actually prefer the goto-based refactoring.

我实际上浏览了一个箭头代码示例(深度嵌套的条件),并在一个版本中使用标准实践(保护子句、分组条件、拉出函数)对其进行重构,在另一个版本中使用基于 goto 的版本,实际上我更喜欢基于 goto 的重构。

回答by David Waters

Are guns evil? Both can be used for good or for evil. I would say it was easier to write good code without goto, than with.

枪支是邪恶的吗?两者都可以用于善或恶。我会说没有 goto 比没有 goto 更容易编写好的代码。

回答by Mark Rendle

Any language feature that can make code more readable in a given situation is A Good Thing. GOTO is one such language feature, even if those situations are few and far between. If we forbade any syntax that made it possible for poor programmers to write bad, unmaintainable code our jobs would be an awful lot harder.

任何可以在给定情况下使代码更具可读性的语言功能都是一件好事。GOTO 就是这样一种语言特性,即使这些情况很少见。如果我们禁止任何让差劲的程序员写出糟糕的、不可维护的代码的语法,我们的工作就会变得非常困难。

回答by Arno

As a software engineer, i mostly work on "mainframes" and "big corporate servers"... And our daily language (I mean the one in 95% of our base code) is Cobol, which uses extensively GOTOs.

作为一名软件工程师,我主要从事“大型机”和“大型企业服务器”的工作……我们的日常语言(我指的是 95% 的基本代码中的一种)是 Cobol,它广泛使用了 GOTO。

This usage doesn't mean the code is bad. It just means that this tool (GOTO) was the right one at the moment programs were written.

这种用法并不意味着代码不好。这只是意味着这个工具 (GOTO) 在编写程序的那一刻是正确的。

To answer Kaitsuli's question, I think it can be useful tool when writing PHP scripts. On the other hand, a lot of scripts were achieved without it for almost a decade by now. Furthermore, it goes against PHP's evolution with more object-oriented features.

为了回答 Kaitsuli 的问题,我认为它是编写 PHP 脚本时的有用工具。另一方面,到目前为止,近十年来,许多脚本都是在没有它的情况下实现的。此外,它具有更多面向对象的特性,这与 PHP 的发展背道而驰。

IMHO, it's nor good nor a bad thing for the code be produced : good programs will still be good and "horror programs" will be worse... The only question is : "Why adding GOTOs 10 years after proving it was not necessary ?".

恕我直言,生成代码既不是好事也不是坏事:好的程序仍然是好的,而“恐怖程序”会更糟......唯一的问题是:“为什么在证明没有必要10年后添加GOTO? ”。

回答by Ignacio Soler Garcia

GOTO usually is evil because it lets you build unstructured code. With the usual loops you can build good structured code easy to follow because it is structured.

GOTO 通常是邪恶的,因为它允许您构建非结构化代码。使用通常的循环,您可以构建易于遵循的良好结构化代码,因为它是结构化的。

When you have non structured code jumping from here to there, you have just found the evil coming from the GOTO statement. Almost always is better to avoid it. Maybe once every 100.000 lines there is a place where a GOTO sentence simplifies A LOT the code thus is not evil but if you are unsure, then you should avoid the GOTO.

当您将非结构化代码从这里跳转到那里时,您刚刚发现了来自 GOTO 语句的邪恶。几乎总是最好避免它。也许每 100.000 行就有一个地方,GOTO 语句简化了很多代码,因此代码并不邪恶,但是如果您不确定,那么您应该避免使用 GOTO。

Hope this helps.

希望这可以帮助。

EDIT: Well, just to add my own opinion here, there are other instructions that allow you to create unstructured code and that are not considered evil when I think they should be.

编辑:好吧,只是在这里添加我自己的意见,还有其他指令可以让您创建非结构化代码,并且在我认为应该是邪恶的时候不被认为是邪恶的。

For example a return in middle of a function is a GOTO to the end of it so I avoid them and use only one return in each function just at its end.

例如,函数中间的 return 是它末尾的 GOTO,因此我避免使用它们,并且在每个函数中仅在其末尾使用一个 return。

Other languages like Vb.Net (maybe others too) allow to do Exit For, Exit While, breaks and things like these that also unstructure the code and I think should be avoid.

其他语言,如 Vb.Net(也可能是其他语言)允许执行 Exit For、Exit While、break 和类似这些也会破坏代码结构的事情,我认为应该避免。

回答by yafrani

sometimes (I mean in 0.01% of cases) it is useful, like when you have a long long script and you want to test some blocks. but never keep it in your final script

有时(我的意思是在 0.01% 的情况下)它很有用,比如当你有一个很长的脚本并且你想测试一些块时。但永远不要把它保留在你的最终脚本中