Friday, September 01, 2006

 

Avoiding the 'g' word


Java has a goto keyword, but it's not implemented. Reserving it prevents programmers from declaring variables named goto (one imagines a default class Goto that works like the goto-guy, used as an ultimate fallback, with a declared instance: Goto goto = new Goto()). Sun is hedging when it reserves the word, just in case they decide to allow it in some future Java release--a bit cowardly it seems to me.

Now there's lots of folks who think you should never, ever use a goto statement, that any program construct can be written using structured programming concepts. It goes so far that I've seen textbooks for langauges that do have goto whose authors declared that they refuse to explain to students how to use it--language Nazis, really.

Of course all of this goes way back to COBOL's heyday, when the only real way to branch (in most languages) was with the goto. And I'm humble enough to admit that I'm old enough to have coded a lot of programs in such a language. The trouble was that these programs degraded over time into jungles of intertwined logic threads. Go here, go there, then go back to here, then go somewhere else. You'd often find programmers crouched on the floor with pages of greenbar all around them and drawing lines in different colors from statement to statement trying to find out just what the original author had in mind.

A bad thing for sure, but you can't eradicate a word from a language by declaring it evil. Goto will always persist in the spectrum of jmp mnemonics at the assembler level. And it will always be useful.

One really good use: to provide a single point of cleanup in a method. For example, say you have a fairly complex function in which you first allocated some resource and then use calls to that resource to initialize a state and then execute a couple more calls to utilize the resource. Being a good little programmer, you obsessively check for error conditions, so you can abort the function if anything goes wrong. But here's the problem: Before aborting, you have to clean up the resource. An easy (and easy to read( way to do this is to place a label at the end of the function, with a name like BAIL_OUT:. Put your cleanup code there and a return. Anywhere, you encounter an error or instability, code goto BAIL_OUT. Easy, clean, efficient, readable, and a lessened probability of error.

Java recognizes that sometimes structured techniques are a pain in the neck, as evidenced by its syntactic feature of labeling loops and then breaking from a labeled loop, rather than setting switches (the structured way) and then checking them on every iteration. But it draws the line at goto.

Arguing over goto is like arguing over fuck. In my view, words exist to be used, if only rarely. Goto, like fuck, ought to be avoided in most contexts, but when appropriately used, it can be a powerful linguistic formulation.

Comments:
Even though I understood about half of that (haven't touched Java for about 4 years), for some reason you made me LOL. Good work =P
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?