tags:

views:

401

answers:

9

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?

+4  A: 

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.

David Waters
Guns don't kill people. Magic missiles do.
Alex
A: 

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

vrutberg
'always' usually implies a bad answer
Carson Myers
'always' always implies a bad answer :)
Remo.D
3 down votes with no comments, how rude :) I did not down vote but always is a hard word to use, how does c implement a loop or if statment? with a JMP (assembly jump) a jump is just a goto. At some level the goto or the jump is the most basic building block. This does not mean you need to use it up at PHP/C#/Java or even C level.
David Waters
@David: Yeah, but that doesn't mean you have to deal with it in your programming. Goto is error prone and suffices from very low maintainability, and it should be avoided. I can not think of one time where I've had no choice but to use a goto.
vrutberg
An harmful device doesn't mean it must be avoided...
Arno
Why would you use something bad?
vrutberg
Becose even for you the goto is the evil, there is some situation that it could be good. Yes, few, really few situation, but that's not the point.
DaNieL
@DaNieL, @vrutberg: escaping multiple nesting of loops is an accepted use of `goto` and it's cousing, `break`, at least on the languages I've used.
voyager
*`goto` considered evil* was written because old **old** school programmers where using it instead of functions, not because `goto` was inherently evil.
voyager
+4  A: 

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.

Remo.D
+1 for the first two paragraphs. Why isn't this the accepted answer instead? :-)
MattBianco
+1  A: 

goto is evil in any language.

However sometimes a little evil is a good thing. muhaaahaahaaa...

Alnitak
+1  A: 

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.

SoMoS
I'm scared to use goto because anyone who reads my code might completely lose faith in me
Carson Myers
I haven't used a goto (except in assembly) for nearly 20 years. Not even when I was writing (DEC) Fortran.
Alnitak
+19  A: 

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.

Konamiman
I would rather say that "if you need to use them *AND YOU DON'T HAVE* them, you are in big trouble".Which is a strong point in favour of having goto into a programming language.
Remo.D
+2  A: 

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.

Coder 42
+12  A: 

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?

Loris
The funniest bit is that his comic is on the php manual reference for goto: http://es.php.net/manual/en/control-structures.goto.php so yes, the raptor WILL chase you, even in PHP ;) also great alt text: "Neal Stephenson thinks it's cute to name his labels 'dengo'"
danii
I would give you +2 if I could (one for being right, one for the comic)
Hippo
A: 

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

Arno