4
$\begingroup$

I just found out something really quite extraordinary. While looking through Stackoverflow, I came across a question about removing goto from a php function. PHP doesn't have goto I thought and looked it up on php.net.

It turns out I was sort of right. PHP introducted goto in version 5.3 which was release in 2009. PHP didn't start out with gotoit actually introduced it into the language in 2009!

Why on earth with all the horror stories we have from 40 years of bad programs written with goto in other languages would php actually decide to introduce it?

The php.net website even has this XKCD image suggesting no programmer should ever use goto.

enter image description here

Question:

What possible reason from a technical point of view could there be for introducing a the goto feature, which I nievely thought had been vanquished from modern computer programming languages?

$\endgroup$
5
  • $\begingroup$ I don't know exactly, but one reason I can imagine is the need to generate code. goto is useful because it's easier to map to from code generator operating on the bytecode level. $\endgroup$
    – wvxvw
    Oct 9, 2015 at 8:39
  • 1
    $\begingroup$ PHP's goto is somewhat different from the "evil" goto as you can only jump to code within the same context (function). $\endgroup$ Oct 9, 2015 at 9:11
  • 3
    $\begingroup$ Are you (a) asking us to divine the intentions of the designers of PHP 5.3, (b) ranting about goto or (c) asking a computer science question about langauge design in general? It seems to me that you're about 50% a, 25% b and 25% c, which isn't a great mix. I think it would be helpful if you could refactor the question a bit to contain more c and less a and b. $\endgroup$ Oct 9, 2015 at 11:09
  • 4
    $\begingroup$ phparch.com/2009/06/goto-in-php-5-3-is-it-really-that-evil this blog post gives some historical references and quotes the developers who introduced goto to the language. They seem to be motivated by the need to generate PHP code from a more general program description. $\endgroup$
    – wvxvw
    Oct 9, 2015 at 12:42
  • $\begingroup$ Duplicate: programmers.stackexchange.com/questions/180068/… $\endgroup$ Aug 30, 2016 at 10:34

4 Answers 4

9
$\begingroup$

There are two kinds of programmers: Those who for whatever reason never, ever use goto, and go to any lengths to avoid it, and those who use goto in one of the very rare situations where it is the best solution. (Someone said here in a comment that goto could be misused and programmers would find ways to misuse it - but those people are not programmers).

It makes sense to add "goto" to a language that doesn't have it if there is evidence that there are situations where it improves the code, and if it is not too difficult to implement. So I would assume that there was such evidence.

$\endgroup$
1
  • $\begingroup$ PHP does not have real arrays and loops are slow, so a few nested loops traversing array searching for something is all evidence needed ;) $\endgroup$
    – Evil
    Oct 28, 2016 at 22:02
8
$\begingroup$

The goto in itself isn't "evil", it is just easy to misuse. A famous paper by Knuth is "Structured Programming with goto statements", ACM Computing Surveys 6:4 (dec 1974), 261-301. For example the Linux kernel uses goto often, in a disciplined and safe way.

$\endgroup$
9
  • 4
    $\begingroup$ Linux kernel uses a handicapped and an extremely problematic programming language, it's more of a miracle that it works so well. Using it as an example to argue for the use of goto is akin to suggesting plagues as means of population control based on how effective they were in the middle ages ;) $\endgroup$
    – wvxvw
    Oct 9, 2015 at 10:34
  • 1
    $\begingroup$ This doesn't answer why PHP introduced the goto statement (especially surprising considering they only added it after such a long time). $\endgroup$ Oct 9, 2015 at 11:16
  • 7
    $\begingroup$ @wvxvw, to call C "handicapped and extremely problematic" in a discussion about PHP boggles the mind. $\endgroup$
    – vonbrand
    Oct 9, 2015 at 16:10
  • 1
    $\begingroup$ @vonbrand that's a false dichotomy. I never said that one thing is not like the other. $\endgroup$
    – wvxvw
    Oct 9, 2015 at 20:19
  • 1
    $\begingroup$ @vonbrand If there's the possibility of misusing a feature, then programmers will find a way. Many ways, in fact. $\endgroup$
    – gardenhead
    Oct 28, 2016 at 19:07
7
$\begingroup$

Most algorithms and forms of business logic can fit nicely into the control structures that are built into modern languages, in large measure because such structures were designed to fit the needs of common applications. Some applications, however, need to implement algorithms or business logic that do not fit any such control structures.

Programs are generally most readable if the general design of the program matches the design of the algorithm or business logic implemented thereby. If the algorithm or business logic jumps around in weird bizarre ways, a program which uses goto to jump around in those same bizarre ways will likely be more understandable than one which has to implement the same logic by adding flags and/or abusing other control structures like switch/case.

Most algorithms and forms of business logic will either fit naturally into common control structures, or can easily be adjusted to do so. If the necessary behavior will fit into a language's control structures--as it will for most applications--it's generally better to use those structures than to use goto. The fact that most programs would receive no benefit from having goto available, however, does not mean that it does not provide great benefits in cases where the required behavior doesn't fit any other control structure.

Incidentally, another factor to consider with goto: many languages are used not only to process code which written by humans and intended to be readable by them, but also to process machine-generated code which is not required nor particularly expected to be human readable. If the behavior of the generated code is described by a series of state transitions, it may be much easier for the code generator to define a label for each state and use goto to hop between them, than for it to try to identify ways of reforming the code to fit the target language's control structures. While some implementations may be better able to process code written using control structures than code written using goto, many others start by converting all control structures to conditional branches (i.e. goto) internally and then analyze the resulting control-flow graph. If a compiler is going to turn a for loop into a bunch of conditional branches, there no reason for a code generator to seek out ways to replace some goto branches with a for loop.

$\endgroup$
4
$\begingroup$

I’ve used goto in exactly one program, not counting BASIC when I was a child. It was to break out of a couple of levels of nested inner loops in performance-critical code, in order to prune a branch of a search tree, without checking a flag in each invocation of all of them. I did it, and I’m not sorry!

I thought at the time that syntactic sugar like break 3; to exit three nested loops would have been elegant and shut people up who complain that goto is Considered Harmful, but of course that’s just a renamed goto. (Now that I think about it again, break continue to break out of an inner loop, then skip to the end of the outer loop, might be equally legitimate.). And at that point, maybe it would start to get confusing where exactly the control flow goes, and it might be more readable to add some kind of comment or label at that line anyway.

The main reason Edsger Dijkstra considered goto harmful is that it makes formal proofs of program correctness impossible. So, while I appreciate that I don’t have to maintain spaghetti code, I do wonder how many of the people who know that rule ever formally prove their code correct.

$\endgroup$
3
  • 1
    $\begingroup$ "just a renamed goto" -- well, yes, but if that's the only kind of goto you allow, you don't get all the problems. $\endgroup$
    – Raphael
    Oct 12, 2015 at 6:42
  • 1
    $\begingroup$ @Raphael Agreed. At the time, what I figured was that using that as my only goto was fine. Someone else brought up the goto to a single point of exit in each function of the Linux kernel. $\endgroup$
    – Davislor
    Oct 12, 2015 at 6:49
  • 2
    $\begingroup$ @Raphael: If the required rules for an input-stream parser require that receiving certain characters when in certain states will require the parser to jump to other states, and the topology of the required state transitions doesn't match any natural cotrol structure, there may be no good substitute for a plain old ordinary "goto". One could use a switch/case state machine to fake one, but if there's no other need for a state machine (versus having the location of the next instruction identify the state) simply using goto will often yield code that's faster and easier to read. $\endgroup$
    – supercat
    Oct 28, 2016 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.