7 comments

[ 0.23 ms ] story [ 18.0 ms ] thread
“Goto Rendered Harmless”
I consider this the official alternative title :)
Even though it’s not the main focus of the article, the sample code's odd structure is a bit distracting.

  int f()
  {
    foo:
        return 0;
    goto bar;
  }

  int g()
  {
    bar:
        return 1;
    goto foo;
  }

  int main()
  {
    return f();
  }

It's treating both "foo" and "bar" like the equivalents of encapsulated subroutines, but the goto statements will NEVER be executed unless there's a C variant or compiler switch I'm unfamiliar with.
My point is exactly that. C is a structured, procedural programming language that will only consider procedurally scoped labels. You can't execute it because it is not valid C that a standard compiler would accept, and we should be thankful because of that.
Goto can be used safely but that’s really just arguing that “harmful” should instead be something like “risky”. If you have robust flow analysis and testing, you can certainly find advantages but it’s kind of like seeing someone doing mountain bike tricks on YouTube and then asking whether you should try that on your commute to work. The Linux kernel developers are in an unusual position of being both extremely performance sensitive and supported with review and testing resources compared to almost anyone else.
goto out makes code so much cleaner and people should stop pretending it doesn't.
As long as memory management and stacks are in order, they are ok to use.

Java still allows you to continue/break to labels