Why is this even a debate?
First of all, strongly held beliefs are rarely a good thing, if ever. So it’s not a bad idea to see why
goto, despite being frowned upon so hard that your brows merge into one, still exists in
C#. Granted, it’s ugly and messy, and a maintenance nightmare, but turns out it can still be useful in a handful of cases.
This article Why goto Still Exists in C# by Steve Wellens is an excellent write-up. There’s not much I can add to it and I pretty much agree with everhthing there. Well everything except that people who use tabs kick puppies. It’s the space indenters that kick puppies, you monsters.
I’m simply reproducing the article below just in case the link goes dead. All credit attributed to Steve.
Why goto Still Exists in C# Language
Developers, Software Engineers, and Programmers are logical, rational, reasonable people right? Sure they are…until you disagree with something they believe in. Then they can become the most enflamed, outraged, foaming-at-the-mouth, intolerant, lunatics you’ve ever had the pleasure of meeting.
Take for instance the
goto command. It can create emotions as intense as those raised during the ancient ‘tabs versus spaces’ debates or whether or not curly braces should be aligned in columns. (For the record, developers who use tabs and don’t line up curly braces also kick puppies and do not practice good hygiene).
You can program for years and never use a
goto. However, there are times when a goto can make the code simpler and that…is a very good thing.
Here’s a scenario: You are working on a multi-threaded real-time program. You can’t use the debugger because stepping through the code would mess up the timings and interactions between the threads. You also need something to help diagnose problems in the field. A runtime log that can be turned on and off is used. The requirement is that every function will log its entry and exit point. That way if something goes wrong, the log will show what function failed and where it failed.
In one area of the program, you need to perform several steps in sequence. If any step fails, the remaining steps must not execute.
Here’s the first attempt:
Sure it works but getting code to work is only the first step. Creating clear, maintainable code is the goal. If the code can be simplified, you are not done.
Second attempt, use a flag variable:
That’s better but it can be simplified further:
Third attempt with goto:
The creation, assigning and checking of a variable has been eliminated. It also runs faster but the speed improvement is insignificant and not a reason for using a goto.
The example is trivial, however in real life, being able to jump to the end of complicated functions can dramatically reduce the complexity of code.
Before you disagree with the inclusion of the goto in the
C# language, remember you are disagreeing with the people who created the language. You are also disagreeing with Steve McConnel the author of Code Complete.
In my career, I’ve only used a
goto once and I had to present the code to a code-review group of four developers. When I showed the code with and without the
goto, they unanimously agreed without any discussion that
goto was… the way to go. Pun intended.
I hope someone finds this helpful.
[Update: July 3, 2009]
Here are two more examples of when a
goto is helpful. The first is from Charles Petzold’s book .Net Book Zero a free pdf book available here .
switch fall through is illegal in
C# (this causes a compiler error):
To get it to work you can use a goto:
This example shows better how to cleanly get out of nested loops/code. The task is to search a three dimensional array and check for a null value: