Is GOTO in PHP evil?

PhpGoto

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

I can't believe nobody posted this :)

[ ![xkcd - goto][2] ][1]

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

[1]: http://xkcd.com/292/ "Neal Stephenson thinks it's cute to name his labels 'dengo'" [2]: https://imgs.xkcd.com/comics/goto.png

Solution 3 - Php

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.

Solution 4 - Php

I think this is the most important part of the PHP manual page and missing here:

> This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one. 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.

IMHO this makes it very different from the ye olde BASIC style gotos.

Solution 5 - Php

I'm in the minority (currently), but I believe the restrictions placed on PHP's goto construct make a very beneficial tool:

http://adamjonrichardson.com/2012/02/06/long-live-the-goto-statement/

I actually walk through an example of arrow code (deeply nested conditionals) and refactor it using standard practices (guard clauses, grouping conditions, pulling out functions) in one version and a goto-based version in the other version, and I actually prefer the goto-based refactoring.

Solution 6 - Php

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.

Solution 7 - Php

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.

Solution 8 - Php

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

Solution 9 - Php

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.

Solution 10 - Php

sometimes (I mean in 0.01% of cases) it is useful, like when you have a long long script and you want to test some blocks. but never keep it in your final script

Solution 11 - Php

I used GOTO when I write script for working under cli mode. It save my life.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTowerView Question on Stackoverflow
Solution 1 - PhpKonamimanView Answer on Stackoverflow
Solution 2 - PhpLorisView Answer on Stackoverflow
Solution 3 - PhpRemo.DView Answer on Stackoverflow
Solution 4 - PhpschmunkView Answer on Stackoverflow
Solution 5 - PhpAdamJonRView Answer on Stackoverflow
Solution 6 - PhpDavid WatersView Answer on Stackoverflow
Solution 7 - PhpMark RendleView Answer on Stackoverflow
Solution 8 - PhpArnoView Answer on Stackoverflow
Solution 9 - PhpIgnacio Soler GarciaView Answer on Stackoverflow
Solution 10 - PhpyafraniView Answer on Stackoverflow
Solution 11 - PhpEThaiZoneView Answer on Stackoverflow