November 5, 2009

PHP 5.3 and the dangers of ‘goto’

khallmark @ 9:14 pm —

PHP 5.3 came out recently. For better or worse, the great PHP gods decided it would be a good idea to add ‘goto’ to the language. Up until now, PHP has never had ‘goto’. The question I’ve been bouncing around in my head is “Why?!”. In my 15 years programming (yes I really have been programming since the age of 10), I don’t think I have ever used a “goto” statement in a real program. Maybe in my first Hypercard stack, or using Basic in my 7th grade computer class, I may have.

My stance has always been, and will always be, ‘goto’ is a crutch for the lazy. It enables bad design. Sure, it might be easier to code one rare algorithm using a ‘goto’ statement. This is not the majority. The majority of algorithms can be implemented without ‘goto’, it just takes more thought. I cannot think of any algorithm that requires ‘goto’.

A lot of php  is spaghetti code to begin with. PHP is one of the easiest web programming languages to pick up, so a lot of new programmers end up writing PHP. There is nothing wrong with this. I myself am guilty of this type of mess. But we don’t need to make it any worse.

So this is the e-mail I received:

I see that as a good thing (the introduction of the goto control structure).

Generally speaking, it is good programming style to use the break, continue, and return statement in preference to goto whenever possible.

However, in certain cases, especially where the correctness of the algorithm is more important, using goto in lieu of break could simplify the implementation of the algorithm.

In such cases, a goto may be necessary for bailing out of a loop from within a deeply nested loop.

I think that is why it was introduced.

I agree with his explanation of why it was introduced. From my personal research, as well as this message, there are supposedly algorithms that exist which for ‘correctness’ involves a ‘goto’. I have yet to see an example. I would like to see one actually.

But I posit, no matter what the algorithm is, it could be implemented without the use of ‘goto’.

I also avoid deeply nested loops . If I must implement an O(n^x) x >= 3 algorithm, I’m going to be separating at least part of the execution into multiple functions. I feel I can use functions and return to effectively control my program execution, without goto. See: the feeds (the channelblade feed was a 4 level nested loop originally).

I feel the entire purpose of functions was to eliminate “goto”. In assembly, a function is simply a labeled section of assembly code. When you call it, you “goto” that section of code. When it returns, it pops the location of the previous memory address of execution and does a “goto” to that position in memory. All this is transparent to you. Functions were designed to abstract this low level machine construct. “Goto” has no place in a higher level language.

Even Dijkstra himself considered the “goto” statement to be harmful to programming. You can read his thoughts from 1968 here:

He argues that the human mind has trouble enough bridging the gap between the program “code” and the dynamic execution. It’s hard enough conceptualizing program flow. Even with recursion, we have induction to help us conceptualize the problem. Once you start introducing goto you are further complicating that program flow; any attempt at understanding a linear program flow becomes an arduous task. It is convenience at the expense of understandability.

I feel that with modern flow control and object oriented program, the goto statement should finally be banished. It is quite simply, unnecessary.

One Response to “PHP 5.3 and the dangers of ‘goto’”

  1. PHP is falling apart (or Why we need ‘finally’) « little black hat – The online home of Kevin Hallmark Says:

    […] ‘Goto’ should not be implemented in any modern language, period. It’s not needed. Yet, PHP chose to implement this in version 5.3! The reasons for adding this were dubious at best, showing a lack of language design skills. You can read all about my opinion on goto here: […]

Leave a Reply

Logged in as . (Logout)