Federico Cargnelutti

Simple is better than complex. Complex is better than complicated. | @fedecarg

PHP: Spaghetti alla Bolognese

with 18 comments

The GOTO statement has been the target of much continued criticism and debate, with the primary negative claim being that use of GOTO results in unreadable and generally unmaintainable spaghetti code. Spaghetti code is a pejorative term for source code which has a complex and tangled control structure, especially one using many GOTOs. As structured programming became more popular in the 1960s and 1970s, many computer scientists came to the conclusion that programs should always avoid using the GOTO statement in programming languages. In 1968, Edsger Dijkstra, a famous Dutch computer scientist, wrote: GOTO Considered Harmful.

Well, great news. The GOTO statement has just been added to the PHP language and it’s now in the hands of experienced programmers and inexperienced Web developers.

Jeff Moore, July 29th 2004:

“Not once in four years of programming in PHP have I thought to myself  – I wish PHP had a goto statement. Perhaps worst of all, I think having goto will cause serious programmers to take PHP less seriously as a language.”

What’s next, COMEFROM?

Written by Federico

January 8, 2009 at 7:10 pm

18 Responses

Subscribe to comments with RSS.

  1. This is not the same goto. This goto has restrictions that prevent the developer from creating big spaghetti code, only small ones. If used inside a function, the target must be inside the same function. If used outside, the target must be inside the same file. It’s actually much more like the branching statements from java (that can have a label and act more like goto when they do) than like the original goto.

    maidenessen

    January 8, 2009 at 7:59 pm

  2. Then it’s not only harmful, but also useless.

    Federico

    January 8, 2009 at 8:05 pm

  3. no encontre otro lugar para comentar. muchas gracias por el programa scriptmaster, me tome la libertad de hacer esto:
    http://www.incavir.com/ivan/scriptmaster.html
    ojala me pudieras comentar algo.

    ivan

    January 9, 2009 at 3:46 am

  4. Where exactly goto has been added to PHP?

    unset

    January 9, 2009 at 8:35 am

  5. And how dare they force people to use it. /s

    Ronald

    January 9, 2009 at 9:42 am

  6. @Ronald

    I lol’d

    Swedish Chef

    January 9, 2009 at 12:00 pm

  7. Well, no one forces people to use the @ operator for example, however a lot of people use it.

    Federico

    January 9, 2009 at 12:34 pm

  8. for one side, they’re trying to bring OOP, and from other they indtroduced Goto!
    People, be afraid, be very afraid. IMHO, Goto statement should be forbidden ;)

    hh

    January 9, 2009 at 12:45 pm

  9. [...] PHP: Spaghetti alla Bolognese « PHP::Impact ( [str Blog] ) Well, great news. The GOTO statement has just been added to the PHP language and it’s now in the hands of experienced programmers and inexperienced Web developers. Published in: [...]

  10. 20 print ‘I’m a procedural *****’ . PHP_EOL;
    31 goto 20;

    Visko

    January 9, 2009 at 3:51 pm

  11. @Visko
    Your code produces an error:

    ERROR LINE (20) Unexpected T_STRING on line 20

    You forgot to escape the single quote with a backslash!
    http://uk3.php.net/types.string

    Federico

    January 9, 2009 at 4:06 pm

  12. What are the pros of having a GOTO statement in a language that is aiming to be OOP? I’m already frustrated enough with picking up php code that is not following some pattern, I can see this being a mess… but maybe there are real situations where GOTO’s are good? I only remember using them with QBasic…

    Andre

    January 9, 2009 at 5:52 pm

  13. I think the PHP hackathons are getting a bit out of control. But, on the other hand, no one seems to care.

    Federico

    January 9, 2009 at 8:05 pm

  14. “maidenessen Says:
    January 8, 2009 at 7:59 pm
    This is not the same goto…”

    Except when the language is abused, and the developer writes the application using a single massive method call. It’s not like we’ve ever seen that happen, eh? Languages are supposed to help you write programs that show locality, elegance, etc. This goes in the opposite direction from that. I’m mainly a Java developer and I don’t actually think that I ever made use of that language’s “goto” statement. In my opinion, if you think that you do, then it’s time to refactor. I suspect the same will be true of PHP as well.

    Zeke

    January 9, 2009 at 8:30 pm

  15. Perl also supports “goto” (http://perldoc.perl.org/functions/goto.html)

    “Well there’s a very limited set of problems where it’s ok. One is generated code, a code generator using goto can be written way better than without goto and nobody is supposed to read that code anyways.”

    http://schlueters.de/blog/archives/94-Goto-your-Christmas-presents-with-PHP-5.3.html

    Christian

    January 9, 2009 at 8:49 pm

  16. > Except when the language is abused, and the developer writes the application using a single massive method call

    True, true…

    > Perl also supports “goto”

    Yes, fortunately we are all using python and php now :)

    Federico

    January 9, 2009 at 9:22 pm

  17. Goto is useful in breaking out of nested loops…. but PHP already has multi-level break, so there is no need for goto. Well, perhaps only to have “break” use labels, not number of loops to break.

    pstradomski

    January 10, 2009 at 12:14 pm

  18. [...] those of you who are unaware of the specifics of the GOTO function, this post, also the post that I learnt this fact from, outlines it. While experienced programmers know enough [...]


Leave a Reply