Ermahgerd goto
-
I have been doing PHP for over 11 years and today someone asked me about goto. Before I even got into why goto is a bad thing, I thought... nah, PHP isn't that bad, surely.
Turns out, PHP does support a limited form of goto. Non-computed only, can't change file or functional scope, meaning it is largely neutered and a non-issue. And it's not entirely bad, check out the manual page for it. Even the PHP devs understand this is a bad idea.
-
Virtually all languages have a form of goto. JavaScript does. C# does.
-
It's the evil ideas version of hello world.
-
Virtually all languages have a form of goto. JavaScript does. C# does.
Yes, but I thought PHP had escaped that particular brand of hell. Oh well, learn something everything day. And the manual page is kinda fun.
-
Meh. if you read my past communiques, you'll know I'm not really that opposed to goto when used correctly to escape inner loops in error conditions.
-
I probably saw it in passing. I've seen so many rants on why goto is bad over the years, compared to a number of 'goto isn't that bad' defences over the year - but then again I do PHP, what the hell would I know?
-
Interestingly, it's quite a recent addition rather than something they're supporting for backwards compatibility. Clearly people must have been bemoaning PHP's lack of the operator.
@PHP Manual said:
The goto operator is available as of PHP 5.3.
-
PHP doesn't need it, it has a numeric argument to break that goes up that many levels to get out.
-
PHP doesn't need it, it has a numeric argument to break that goes up that many levels to get out.
Nuh uh...
*researches*
Yuh huh. :O
How did I never encounter this?
-
That's how it works in JS too. However, "break to a label" is just window-dressing for goto. IMO. Which is correct.
EDIT: oh wait that isn't really how it works in PHP at all. Huh. How can the code below tell it's been broken-to from the inner loop and not the outer? Or I guess it just can't?
-
No, break-to-label is how Java and Perl do it. This is like:
while (true) { while (true) { break 2 } }
to break out of both loops.
-
<?php eval("a: goto a;"); // works ?>
This is the worst PHP code I've ever seen.
-
Virtually all languages have a form of goto. JavaScript does. C# does.
Javascript doesn't have goto.
Or am I missing something?
-
-
When we're talking about goto, we're referring to the concept of "jumping to an arbitrary label in the code". It doesn't matter whether that's done with the word "goto" or not.
JavaScript's "break label" syntax is a goto. It's also specifically designed to be used in the one place where a goto is appropriate: breaking out of inner loops.
-
It's also specifically designed to be used in the one place where a goto is appropriate: breaking out of inner loops.
Waiting for the "multiple returns from a function are evil" argument to commence...
-
Meh. if you read my past communiques, you'll know I'm not really that opposed to goto when used correctly to escape inner loops in error conditions.
I did this once in some usermode driver C code and got pounced on for it. Ended up re-writing it without the goto, and given that it was C and was diving into the Windows registry to find things and C's "string" functions suck and stuff, along with some extra booleans to track if the innermost loop aborted and that needed to bubble up to the outer loops, it added 10 - 15 lines of code when the single goto was removed. Not a huge deal, except removing lines of code from a C program is always a good thing.
-
I don't consider that a goto. Problem with goto is that you could jump literally anywhere, disregarding local loops, function scopes or anything else.
break
, on the other hand, is a structured jump instruction, that throws you to a well defined predictable spot in code, staying within the function boundaries.Saying that
break;
is like goto is like saying thatif
is like goto because you jump to a different part of code.
-
What he's talking about isn't
break
, it'slabel: [snip code] while(true) { break(label); }
Where "label" could be anywhere. It's a jump to an arbitrary point, from inside a loop or a block statement of any kind. I can't seem to find is any indication of limits on where the label can be placed, either...
-
I know.
You can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.
Note that JavaScript has no goto statement, you can only use labels with break or continue.
So, this is just a way to pinpoint an outer loop when you use
break
orcontinue
. You can't even place it to an arbitrary spot within a function, let alone outside of it.
-
ah, that's what I was looking for, is where it can be placed.
-
Waiting for the "multiple returns from a function are evil" argument to commence...
A single return statement in a function is usually evil...
-
Maciejasjmj said:
Waiting for the "multiple returns from a function are evil" argument to commence...A single return statement in a function is usually evil...
No returns in a function are also evil. At least in PHP when there's no return signature and you forget to put the return in and then wonder why so many things are broken....
-
int square (int x) { return x*x; return -1; //don't be evil }
-
No returns in a function are also evil. At least in PHP when there's no return signature and you forget to put the return in and then wonder why so many things are broken....
Any real language would require
void
.
-
```
int square (int x)
{
return x*x;
return -1; //don't be evil
}`return EVIL_NOT_FOUND;` #EDIT: ANOTHER DISCOURSE BUG!!!!!!!!!!!!!!!!!!!!!
-
-
Figured as much.
-
Did you know about these shenanigans, @Arantor?
Yes, I knew about break and even break num to break a given number of levels. At one time you could even break $var to break the number of levels in $var but I believe they removed that.
It even came up on the Zend exam!
-
@chubertdev said:
ANOTHER long ago reported and won't be fixed any time soon DISCOURSE BUG!!!!!!!!!!!!!!!!!!!!!
FTFY
Didn't they say that this had something to with stripping formatting out of quoted text? And how that was intentional?
-
Yes, it is intentional. It doesn't change the fact that it is stupid and deserves to be called a bug by any sane software developer.
-
Just making sure my memory wasn't failing. Hard to keep track of all these damn bugs.
-
It's one thing to strip formatting out of quoted text. It's another to leave in the formatting tags, let them take precedence, and summarily break the quote.
-
Yeah, but I think the two are connected in Dicsourse.
-
multiple returns from a function are evil
I got that lecture during my early days at a Delphi shop. It seemed like a good idea until you had to create several booleans and nest several if blocks to achieve something less readable than with multiple exit points.
-
I got that lecture during my early days at a Delphi shop. It seemed like a good idea until you had to create several booleans and nest several if blocks to achieve something less readable than with multiple exit points.
I would beat someone with a live trout if they preached that to me in person.
-
I never got that lecture in any programming class, but I had to live it thanks to starting with Classic VB.
-
As with most things,
goto
is not bad by itself. It's how its used that makes it bad. But it's easier to tell people to never do something than to do it properly, apparently.Like @ChaosTheEternal, I had to do the whole 'single exit point using booleans and other shenanigans' stuff because of classic VB, and frankly, multiple return points when appropriate are infinitely easier to read.
Some people just have no business writing coding standards.
-
As with most things, goto is not bad by itself. Its how its used that makes it bad.
A bit like it's and its.
-
-
I was about to comment that he had lost that apostrophe down into the next paragraph by accident, but then he ninjafixed it.
-
Yeah, saw your ninja edit.
At least you're not a chronic abuser like @Onyx
-
Like a ninja in the night, Onyx, AKA Hanzo, stalks across The Daily WTF forums. The go-to man in the post editing department, he fixes the messes that he leaves behind. These are his posts.
-
-
I would beat someone with an
liveevil trout if they preached that to me in person.FTFY
-
@trithne said:
It's how its
You're still missing an apostrophe. ;)
Still missing 9 hours after @Keith's post.
-
And how would it work in a language with
throw
?throw
is kind of likereturn
. Having multiple would be similarly confusing (following the reasoning of those who argue against multiple returns).Can you have a
throw
and areturn
? Can you only have one or nothrow
s and one or noreturn
s?Are proponents of single return all coders who worked on languages without multiple return long ago and when they first encountered a language with multiple return were confused at first and decided they hated it because there was a bug because they didn't keep in mind that a procedure could return in multiple places and would never forgive multiple returns and have been harboring resentment toward the concept ever since?
-
-
-