This article gives a short overview of why GOTO can be useful, why we have it in PHP (since 2004) and why it was/is controversial. The general answer seems to be: GOTO is mostly useless and should be avoided unless in very narrow application spaces (like building a compiler):
One thing is certain: there are some application in which a good developer can put GOTO to good use to produce code that is simpler and more efficient, but they are probably not the kind of mainstream programs that your average developer will be writing; therefore, there is a legitimate concern here that inexperienced programmers will use GOTO to the general detriment of their code.
Also see some of the original newslist discussion/controversy mentioned in the article:
One case where I use it is when I have multi-level checking to do and don't want to add extra code when I could just jump to the relevant section after my checks.
For example, there's this system that checks the status of various machines in a warehouse, and fails the warehouse as a whole and sends out an alert if any one part of any one system is dead, in pseudo-code it goes like this:
That way, if anything fails, I can skip all other checks and go straight through to the alert. Whoever is on call receives the alert and pulls up the full result of whatever function called the alert so that they can fix it.
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 has been in large replaced by specialized statements like continue, break, try...catch. In the years I'm working with PHP, I didn't feel it could help me once. In languages like C++ which don't have break x statement it can be used to jump out of nested loops, but since PHP has it, there is no reason.
Code Complete has very nice and detailed chapter about gotos. Quoting the final two paragraphs:
Use of gotos is a matter of religion. My dogma is that in modern languages, you can easily replace nine out of ten gotos with equivalent sequential constructs. In these simple cases, you should replace gotos out of habit. In the hard cases, you can still exorcise the goto in nine out of ten cases: You can break the code into smaller routines, use try-finally, use nested ifs, test and retest a status variable, or restructure a conditional. Eliminating the goto is harder in these cases, but it's good mental exercise and the techniques discussed in this section give you the tools to do it.
In the remaining one case out of 100 in which a goto is a legitimate solution to the problem, document it clearly and use it. If you have your rain boots on, it's not worth walking around the block to avoid a mud puddle. But keep your mind open to goto-less approaches suggested by other programmers. They might see something you don't.
I find it very hard to understand why the PHP team bothered to add it at all (after all those years we lived in peace without it).
发布评论
评论(5)
一种可能的用途(但是可以使用其他方式实现)是实现有限状态机。
One possible use (that can however be be implemented using other means) is implementation of a finite state machine.
请参阅 PHP 架构师 - PHP 5.3 中的 GOTO:真的那么邪恶吗?
本文简要概述了为什么 GOTO 很有用、为什么我们在 PHP 中使用它(自 2004 年以来)以及为什么它曾经/现在是有争议的。一般的答案似乎是:GOTO 几乎没有用,应该避免,除非在非常狭窄的应用程序空间中(例如构建编译器):
另请参阅文章中提到的一些原始新闻列表讨论/争议:
另请参阅有关 GOTO 在 C2 Wiki 中被认为是有害的
See PHP Architect - GOTO in PHP 5.3: is it Really That Evil?
This article gives a short overview of why GOTO can be useful, why we have it in PHP (since 2004) and why it was/is controversial. The general answer seems to be: GOTO is mostly useless and should be avoided unless in very narrow application spaces (like building a compiler):
Also see some of the original newslist discussion/controversy mentioned in the article:
Also see this thorough language agnostic discussion about GOTO considered harmful at C2 Wiki
我使用它的一种情况是,当我需要进行多级检查并且不想添加额外的代码时,我可以在检查后跳转到相关部分。
例如,有这样一个系统,它检查仓库中各种机器的状态,如果任何一个系统的任何一个部分出现故障,整个仓库都会出现故障,并发出警报,在伪代码中它是这样的
:这样,如果任何事情失败,我可以跳过所有其他检查并直接进入警报。无论谁值班,都会收到警报并提取调用警报的任何函数的完整结果,以便他们可以修复它。
One case where I use it is when I have multi-level checking to do and don't want to add extra code when I could just jump to the relevant section after my checks.
For example, there's this system that checks the status of various machines in a warehouse, and fails the warehouse as a whole and sends out an alert if any one part of any one system is dead, in pseudo-code it goes like this:
That way, if anything fails, I can skip all other checks and go straight through to the alert. Whoever is on call receives the alert and pulls up the full result of whatever function called the alert so that they can fix it.
goto
运算符可用于中断多级循环。这个特定的
goto
应用程序示例在goto手册中给出页面:goto
operator can be used to break multi-level loops.This particular
goto
application example is given at goto manual page:Goto 已在很大程度上被专门的语句所取代,例如
continue
、break
、try...catch
。在我使用 PHP 的这些年里,我从来没有觉得它对我有帮助。在像 C++ 这样没有break x
语句的语言中,它可以用来跳出嵌套循环,但既然 PHP 有它,那就没有理由了。Code Complete 有关于 goto 的非常好的详细章节。引用最后两段:
我发现很难理解为什么 PHP 团队要费心添加它(毕竟这么多年我们在没有它的情况下和平地生活了)。
Goto has been in large replaced by specialized statements like
continue
,break
,try...catch
. In the years I'm working with PHP, I didn't feel it could help me once. In languages like C++ which don't havebreak x
statement it can be used to jump out of nested loops, but since PHP has it, there is no reason.Code Complete has very nice and detailed chapter about gotos. Quoting the final two paragraphs:
I find it very hard to understand why the PHP team bothered to add it at all (after all those years we lived in peace without it).