Hacker News new | ask | show | jobs
by steve8918 5019 days ago
I've been programming for around 20 years now, and I've very, very rarely seen goto statements used, except in the case of error handling (which, in my opinion, was a great use of goto). So, I think as an industry, we've done a good job in curtailing the use of goto, so the article really shouldn't have bothered mentioning it, because it's more like a red herring than anything else.

However, I've seen a whole crapload of terrible code. You don't need goto to create code that jumps around, and where the state is very hard to determine. That can easily be caused by things like global variables, badly designed code, etc.

7 comments

I've been programming for about 21 years now, and I would like to echo points so eloquently expressed by this junior said :)
> I've been programming for around 20 years now

Me too.

> I've very, very rarely seen goto statements used

I saw them used all the time in Basic =)

> You don't need goto to create code that jumps around

Nope. All you need is a loop, with lots of continue/break statements mixed in. I have seen this quite a bit and I start twitching when I see it. That's the modern equivalent of the goto.

Even worse when the code uses labelled breaks and continues.
I found one in the PostgreSQL source not to long ago. It was well commented as to why it existed, was well modularized and quite frankly appropriate.

I use "spaghetti code" to refer to code where execution aimlessly wanders between apparent modules. You don't need goto statements to make it happen. SQL-Ledger is a good example of doing this with simple redefinition of functions. It isn't the only kind of bad code though.

GOTO can be quite reasonable when dealing with a language that lacks exceptions. The Linux kernel CodingStyle document says:

"The goto statement comes in handy when a function exits from multiple locations and some common work such as cleanup has to be done."

http://www.kernel.org/doc/Documentation/CodingStyle

IMHO the only acceptable use of a goto statement is to break from a multiply-nested loop in C-like languages. Even there, it is likely that a refactor can eliminate this. It's usually a sign that there is a simpler or more elegant way of doing things.
I've been programming for 30 years and for about 20 years I haven't seen many gotos so I agree, it's a problem of the past.
Messy GOTO code is alive and well in the form of abusing exception handling.