X
Tech

If PHP then goto is the future

Few things can spark more religious fervour amongst programmers than the mention of a goto statement.
Written by Chris Duckett, Contributor

Few things can spark more religious fervour amongst programmers than the mention of a goto statement. PHP has stepped into the middle of the firefight by announcing that it will be implementing goto functionality in version 5.3.

There is plenty of discussion surrounding PHP's decision, with PHP founder Rasmus Lerdorf defending the move, stating it is no different than breaking to a label and that it will not be an unlimited goto (it will not let jumps into loops or switches).

Despite Lerdorf's assurances, the wailing and moaning continues relentlessly. Why is this? To understand, one must realise that the vast majority of programmers have been taught that goto is considered harmful, is a bad idea and will bring pestilence to the land if used. Most students are happy to accept this, generally based on the experience of writing ridiculous spaghetti code in BASIC, so they move onto structured programming and eventually object-orientated programming.

This happens for very good reasons; a maturing programmer has more chance to get themselves in a bind by being unstructured and it is generally better for maintainability and understanding to avoid goto.

By going against the goto grain, PHP is challenging a tenet upon which the foundation of modern programming is built. The size of the challenge is only compounded by the fact that the change is occurring in version 5.3 of the language, a long time after the language muffed but eventually reached satisfactory attempts at implementing objects, and appears to be a step backward.

xkcd-goto.png

xkcd says it best on bad uses of goto (Click image to enlarge)
(Goto image by xkcd, CC2.5)

Here's a shock for the young, blindly-accepting-articles-of-faith type of programmer that has never had to use a goto: most high-level languages implement a form of goto, you just know them as break, switch and continue. And that most-revered of code projects, the Linux kernel, makes extensive use of the goto statement and is written in C — the most respected of languages.

What is good enough for Linus and the team of kernel hackers should be good enough for Rasmus and his clan of web developers.

It's true that goto is a dangerous weapon and in the hands of the inexperienced it's a devastatingly head-scratching weapon of spaghetti creation, but in the right hands it can remove the maze of nested branching structures nicely.

What does this all mean for PHP? Not much. Those programmers adverse to using goto will continue to avoid it at all costs (no one is forcing them to use it), quality programmers will be able to use it well and sparingly, and the bad programmers that create spaghetti will do so even more now that they can use the statement.

But they were going to create bad programs anyway, goto simply made it easier for them to get into a mess. They'll learn eventually.

Editorial standards