Is GOTO in PHP evil?

Viewed 47016

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 GOTO is evil and implies bad code?

11 Answers

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.

I can't believe nobody posted this :)

xkcd - goto

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

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.

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

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.

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.

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.

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.

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.

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 ?".

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.

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.

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.

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.

Related