In reference to Sara's patch to implement GOTO in PHP:

It saddens me that people are so easily brainwashed by their teachers/peers. Sure, from a high level perspective you don't need a GOTO statement, and the argument that it, when misued, can lead to unreadable code is a good one.

However, I feel that the inverse argument can also be true: complex code with lots of nested if statements and control loops can be also be hard to follow when you need to "get out of jail" fast. Without goto you often need to resort to tricks (in C) such as "do { ... break; ... while (0)", or if you have nested switch's inside loops, create a flag variable and take several steps out of each nesting level. There is no way in hell you can convince anyone of sound mind that those tricks make the control flow easier to understand than a GOTO.

No doubt the anti-goto brigade will campaign that the code could be split up into different functions etc. etc. etc., but it is quite often the case that real programmers need to have this stuff in one function; just take a look at kernel code written in C, for example.

Arguments against GOTO on the following grounds are, IMO, just a bit short-sighted:

Regardless of who wrote the paper, or what you were taught, or your disregard for performance (which does have a big impact in the real world; Exceptions are expensive), GOTO is sometimes needed to get the most out of your code. I'll even go so far as to say that most of the people making these anti-goto comments have no practical experience either of using goto, or with dealing with someone elses misuse of goto; if you don't have experience, please shut the hell up :-)

GOTO is one of those tools that you don't always need, and one where it is hard to define precisely when you should use it (<insert anecdote about professor who determined only one extremely rare case you can't handle without goto>). It boils down to what is appropriate for the task (and language) at hand.

Use your brain and evaluate the project--don't just blindly follow what others have said in the past; make up your own mind. If you truly believe that a goto makes a certain section of code more readable, go ahead and use it. If you deduce from benchmarks that exceptions, nested loops with flag variables or whatever are too slow or otherwise too expensive, you have have a perfectly valid reason to use a goto instead.

I'll sum up with a "GOTO doesn't kill code, bad programmers do". Just because misuse of GOTO can be harmful, it doesn't mean that it shouldn't be part of your toolbox, and for that reason I would welcome a GOTO statement in PHP.

View Comments