GOTO considered harmful. Except for…

By

December 27, 2014General4 Comments

The goto is considered harmful. In general. If anyone finds a goto keyword somewhere in a code base nowadays, the default action is to identify the author of this code line and instantly assassinate him. It’s safe, because jury always sides with heroes who do good for public.

Java doesn’t even have this keyword, probably in a bid to maximize life expectancy for Java developers.

But you know, software development is not only Java. Someone still uses C, there are a lot of fellas doing C++ out there, and they still have possibility to compile code with plenty gotos in it. Damn, even C# has goto keyword. And because C# is younger than Java, it is supposed to learn from Java mistakes and be more evolved, at least in a sense of language syntax.

Yet C# has goto.

 

Explanation in MSDN is naïve. It says goto might be useful for a) control switch statements or b) exiting deeply nested loops. Both statements don’t hold water, because:

  1. Thou shalt not reuse code by jumping up and down the switch case blocks with goto’s. It’s just incredible dangerous. Use procedures! People have used procedures since 1950ies, it’s a classic technique. Or use fallthroughs. Fallthroughs are cool too.
  2. Thou shalt not exit loops with goto. Use return instead!

The high-performance code where function call overhead is a problem inevitably gravitates towards unsafe code. If C# language designers included goto for addressing high-performance problems only, they would have limited this keyword for usage in explicitly unsafe code blocks only (you know, the blocks where you can use pointers).

So in short: exiting deeply nested loops should NOT happen with goto. Code reuse within switch statements should NOT happen with goto either. I’m sorry C# developers, the C# language designers have sinned. Jury is on your side.

 

 

…Where is goto applicable then?

 

 

The only justifiable usage for goto statement I’ve ever seen was in a modern ANSI C code. It was where a procedure made a lot of critical resource allocations first, and had to perform cleanup for each resource before exiting.

  • Because C hasn’t got finally clause, you cannot use return + finally as you could in Java or C#.
  • Because C has no exceptions mechanism, each error checking is done manually – by verifying return codes.
  • Because C has no object-oriented stuff (like objects, constructors and destructors), you cannot use Scope-based Resouce Management.

Here’s the code without goto:

 

Clearly, this code violates DRY (identical code for resource cleanup before the business logic and after it).

 

Could we do have the same functionality without duplicated code AND without goto? Sure! Many ways possible. But all are ugly and extremely verbose. Usage of goto in this scenario is okay. Thou shalt not kill software developers who code in C and use goto for this particular case only.

 

A takeaway here? Goto considered harmful. Period. Small asterisk: unless you use C and have to deal with a lot of resource cleanups and don’t want your code duplicated all over the place.

 

4 Responses to “GOTO considered harmful. Except for…”
  1. Jev

    Hi,

    Firstly, as I remember (correct me if I am wrong) Java has labeled breaks, which basically the same as goto in C#. What C# did is just chose good name for these labels.

    Another point is that switch “fall through” is illegal in C#, so you cannot use

    switch (a)
    {
    case 3:
    b = 7;
    case 4:
    c = 3;
    break;
    default:
    b = 2;
    c = 4;
    break;
    }

    But you can use goto instead.

    switch (a)
    {
    case 3:
    b = 7;
    goto case 4;
    case 4:
    c = 3;
    break;
    default:
    b = 2;
    c = 4;
    break;
    }

    Does it sound reasonable?

  2. gugit

    Oh my god… C# language designers are really solving the well-known technique called “fallthrough” with evil goto?

    • Jev

      Historically switch “fall-through” is one of the major source of bugs, so C# language designers decided to forbid it to avoid accidental fall-through. If you want it, then say it explicitly using got statement.

      But all in all, it’s the same as arguing about whether or not curly braces should be aligned in columns or ‘tabs versus spaces’ debates. IMHO in same cases gotos can lead to more readable and simplistic codebase.

      Some references:

      In some cases, gotos can result in faster and smaller code. Knuth’s marvelous 1974 article cited a few cases in which gotos produce a legitimate gain (Knuth 1974).

      Good programming doesn’t mean eliminating gotos. Methodical decomposition, refinement, and selection of control structures automatically leads to goto-free programs in most cases. gotolessness is not the aim, but the outcome, and putting the focus on no gotos isn’t helpful (Code complete, Steve McConnel)

      • gugit

        “gotolessness is not the aim, but the outcome, and putting the focus on no gotos isn’t helpful.”

        Steve McConnel shot a good one here. I like how it sounds. Substitute “goto” with “code smell” (or just about any code smell out there) and you get nice strong controversial expert opinion!

        “Absence of god objects is not the aim, but the outcome, and putting the focus on no god objects isn’t helpfull.”

        “Absence of code smells is not the aim, but the outcome, and putting the focus on no code smells isn’t helpfull”.

        “Absence of world’s worst practices is not the aim, but the outcome, and putting the focus on eliminating world’s worst practices isn’t helpfull”.

        B-)

Leave a Reply