Tuesday, July 7, 2009

GOTO Hell

It seems that people have forgotten what Dijkstra said about GOTO. You see GOTO is actually being added to languages that did not offer it before (PHP5). Have we not learned that GOTO is too easy to abuse?

The argument against GOTO (as given by Dijkstra) is simple, when you use GOTOs you cannot tell without a great deal of work what a program executing has done. Example if you use the following:

n = 783
:start
i = 2

:loop
if n%i == 0
goto done
end
i = i + 2
if i > n
n = n + 2
goto start
end
goto loop

:done
print n
n = n + 2
if n < 100000
goto start
end

This program would not be very hard for most people to figure out what is going on, but what if somewhere deep in the source code called start or done--or even worst loop. What would happen if you call loop or done much latter in the code? Are you sure you know what the values of i and n would be? I agree GOTO does have it uses, I mean sometimes it can save you a lot of code (see the C code for ls). GOTO is at its best when it gets you out of a deep if/for/if/case/... structure, but you could always use private methods in order to get yourself out of those cases without a GOTO. You see there is no real reason to use a GOTO and as such I believe if the greater good it is best to leave it out of languages.