The goto sin

This is the best rant on the demise of the goto statement I have ever heard. It is from the Tango conference 2008 – Fibers talk by Mikola Lysenko. If you fast forward to 23 min 05 secs, you will hear this:

One way you can think about states [in a state machine] is that they’re kind of a label. And you put this label here for where you want the code to goto after you’re done with this other, sort of, state. A state machine is kind of an indirect goto and so these states and switch statements are just like nested gotos within gotos.

Now if you use coroutines, you can then use structured programming to represent the states. I mean this is a debate that was you know, played out years ago in a more limited context of structured programming versus gotos and ultimately, structured programming won out.

Nowadays I mean if you go into entry level programming course, they’ll just fail you on your projects if you even use a goto statement because those goto statements are that toxic to the integrity of programming code. You’re much better off using structured programming. And yet despite that, people still advocate using these state machines which are basically a really indirect horrible obfuscated goto mess, just split across multiple source files and larger projects.

So it’s like if you make the goto sin big enough, then no one is going to call you on how bad it is! But the thing is if you use coroutines, not a problem! So why have we been using this all along? Oh once again I’d probably appeal to the fact that, ah, you know, ignorance, right, people just don’t know about coroutines.

Now I want my goto back… er… coroutines!

Advertisements

2 Responses to The goto sin

  1. I’m partial to Linus Torvalds’ rant on the demise of goto. Always makes me smile


    > However, I have always been taught,
    > and have always believed that “goto”s
    > are inherently evil. They are the creators
    > of spaghetti code

    No, you’ve been brainwashed by CS people who thought that Niklaus Wirth actually knew what he was talking about. He didn’t. He doesn’t have a frigging clue.

    Well said that man. See http://kerneltrap.org/node/553/2131 for more.

    Cheers
    David

  2. Paddy3118 says:

    Some languages accumulate features, so C++ will have OO and templates sitting on top of goto. Other languages abstract features, removing features that don’t fit their abstraction.

    Some algorithms might be ‘better’ coded with one feature or another, but it wouldn’t be right to expect every programming language to support a feature such as goto.

    It is wrong however for a lecturer to mechanically deduct marks for the use of any feature. If used correctly, and that use explained, then I might award extra points to a student using goto in C for an implementation of a state machine – but only if such an implementation was demonstrably better than the others avoiding the use of goto.

    In general though, I am quite happy that educators de-emphasize the use of goto in languages that have the statement.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: