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?
views:
401answers:
9Are 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 always implies bad code, no matter what situation or context. It makes the code harder to read, follow, maintain and debug. I'm not sure why they chose to implement it in PHP 5.3.
Here's a related question http://stackoverflow.com/questions/723324/php-and-the-goto-statement-to-be-added-in-php-5-3
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 it's easy.
goto
is evil in any language.
However sometimes a little evil is a good thing. muhaaahaahaaa...
GOTO usually is evil because let you build unstructured code. With the usual loops you are able to build good structured code that is easy to follow because is, just that, structured.
When you have code that is not structured and that can jump from here to there you have just found the evil coming from the GOTO sentence. Almost always is better to not use it, maybe once in a 10.000 lines there is a place that a GOTO sentence simplifies A LOT the code and is not evil but if you are unsure do like me and forget about GOTO :)
Hope this helps.
EDIT: Well, just to add my own opinion here, there are other instructions that allow 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.
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.
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.
I can't believe nobody posted this :)
Granted, PHP is not compiled... Maybe the raptor will chase you on every visit to your website?
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 ?".