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:
- 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.
- 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:
int resource1 = acquireResource1();
int resource2 = acquireResource2(resource1);
int resource15 = aqcuireResource15();
// now for the business logic
int result = invokeSomething(resource1, resource 12);
// jump to resource cleanup
// cleanup logic
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.