Jeffrey Palermo (.com)

Sponsors

The Lounge

Wicked Cool Jobs

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
The condemned "goto x;" jump statement - level 200

I've used all of the C# Jump statements.  You probably use break, continue, and return on a daily basis.  And in your switch statements, you probably use default.  But do you use the goto statement?  If you are like me, you've been conditioned since QBasic to avoid the infamous goto statement.  It creates spaghetti code, right?  Well, if you use it irresponsibly, it does, but what about nested if statements?  How do you jump out of 2 nested if statements?  break and continue don't work.  My solution:  Use the goto statement.

<snippet>
if(condition 1) { statement; statement; if(condition 2) { statement; goto endIf; } } statement-that-should not happen if condition 2 is not met; endIf:; //label to jump to and empty statement included.
</snippet>

You can probably think of several ways to refactor the above in order to avoid the goto statement, but in this case, the goto statement was the best decision, and this is a very valid use of the statement, and it does not create spaghetti code.


Posted Mon, Jun 7 2004 10:10 AM by Jeffrey Palermo

[Advertisement]

Comments

James Curran wrote re: The condemned "goto x;" jump statement - level 200
on Mon, Jun 7 2004 8:22 AM
"and it does not create spaghetti code."

Define what you mean by "spaghetti code".

Unlike "break" & "continue", "goto" giving you no indication where the execution is going to continue. It could be up or down or anywhere.

Doing that also confusing (for the programmer) object lifetimes, and undermines the compilers ability to optimize the code.
Mark Bonafe wrote re: The condemned "goto x;" jump statement - level 200
on Mon, Jun 7 2004 8:29 AM
You are most definitely creating spaghetti. I cannot understand why anyone would do this. Simply put the "statement-that-should not happen if condition 2 is not met;" inside the first if condition above the second if condition.

If that is not good enough, then you should consider creating more than just one method so you don't have to "jump" all over the place in your code. You obviously have more than just one purpose for the code. So creating extra methods, one for each purpose, should solve the problem.
Jeffrey Palermo wrote re: The condemned "goto x;" jump statement - level 200
on Mon, Jun 7 2004 8:30 AM
Spaghetti code is code that is hard to follow. It does jump around up and down and all over the place. My example of the goto statement is very straight-forward and is used to jump out of a specific IF statement. What method would you prefer to jump out of nested IF statements?
Jeffrey Palermo wrote re: The condemned "goto x;" jump statement - level 200
on Mon, Jun 7 2004 8:37 AM
I'm using the goto to simply jump out of the if statement. By my label location, I just down a few lines, that's it. One jump. Straight-forward.
I cannot move the statement-that-should not happen if condition 2 is not met; inside the first IF condition because it has nothing to do with the first IF. It's independent. If both condition1 and condition2 are true, this statement should be skipped. I obviously could avoid the goto with another if checking for them both, but I prefer this simple goto usage. I think this is an appropriate use of goto, and I will defend it. The writers of the C# language thought the goto statement worthy of inclusion, so what argument is there for comdemning the goto statement to a life of nonusage? It goes without saying that just because it's there does not mean it has to be used. Same with xml. It doesn't need to be used with everything, just where appropriate. Same here. Use goto where appropriate.
Mark Bonafe wrote re: The condemned "goto x;" jump statement - level 200
on Tue, Jun 8 2004 1:36 AM
I cannot explain it any better than James. His comments should be all you need to understand why it should be avoided.
Jeffrey Palermo wrote re: The condemned "goto x;" jump statement - level 200
on Tue, Jun 8 2004 2:18 AM
In regards to James's comment, I'd be interested to know what compiler optimization is precluded by the usage of goto. Mark, it sounds like you will never use the goto statement for anything. I'll use it where appropriate and where it makes sense.
Brian Orrell wrote re: The condemned "goto x;" jump statement - level 200
on Wed, Jun 9 2004 4:37 AM
It's just bad. Can you not accept that there is NOT a use that is considered acceptable by your peers?

It's not good practice. Period. And the C# team included the goto statement (my guess) so that VB programs could be migrated more easily.

If you want to get the same label that poor VB programmers have had for a long time (writing crap code) have fun.

Show me one, ONE MS guy who says that he can see a valid use for you using it in production code.

It's not a best practice.
Noah Coad (C# MVP) wrote re: The condemned "goto x;" jump statement - level 200
on Thu, Jun 10 2004 2:24 PM
I would tend to agree with Brian. I try to avoid using the statement, if only because it is so controversial. I have tried using goto in programs before, but have found with a little more thought and perhaps an extra line of code, it is worth trying to avoid.

The statement breaks down the natural flow of logic. For the same reason, I try to avoid "break" and "continue" (I never use "continue"). I try to layer my logic to avoid such circumstances.

When I get to the Visual Studio team at Microsoft in July, I can ask around. I will also ask someone from the C# team to take a look at this post right now.

Here is an article on how GOTO is harmful. http://www.acm.org/classics/oct95/
Jeffrey Palermo wrote re: The condemned "goto x;" jump statement - level 200
on Fri, Jun 11 2004 2:29 AM
I read the above article, and I agree with it 100%. The author's position is to use extreme caution with goto because it an lead to bad code. I agree with this. This comment thread has been very interesting for me because of the viewpoints expressed. I'm certainly not one to say that I know best - period! I encourage feedback and learning. What I have gathered from the comments is that goto is just bad - period. While I agree with the arguments against common use of goto, I'm struggling to believe that an included feature in C# is really taboo. I made this post because for the first time in my development career I'd thought I'd found a legitimate use for the goto statement. I've gathered that most people avoid goto at all costs. In several years I aspire to be a .Net trainer, so from that perspective, I have to be able to answer the question: Why shouldn't I use goto? I can't just give the answer: It tempts you to overuse. It makes the program confusing if overused. It promotes bad code. It's hard to follow. I can see the students reactions to those answers, so what I'm looking for is a real answer. What is the REAL reason not to use goto? Why is it in C#? Noah Coad has contacted Eric Gunnerson to solicit his opinion on the matter. We'll get to the bottom of this eventually.
Jeffrey Palermo wrote The goto statement revisited - level 200
on Wed, Jun 30 2004 6:51 AM
The goto statement revisited - level 200
Michael Fitzpatrick wrote re: The condemned "goto x;" jump statement - level 200
on Wed, Jun 30 2004 8:33 AM
I use GOTO all the time in C code. I had to to implement exception handling using error code return values. I will occasionally use GOTO for breaking out of nested loops in C. I use GOTO in VB in place of break and continue, which aren't supported in VB.

In C++ there is SEH, but it is expensive. I have a lot of C code that I ported to C++ that still uses GOTO for exception handling.

I think that if a programmer has done assembly coding that they loose religion about the use of GOTO real fast. The point is that any coding construct can be abused. GOTO has gotten a really bad rap because it was overly abused, and college professors don't allow it at all.
Jonny wrote re: The condemned "goto x;" jump statement - level 200
on Sat, Aug 28 2004 4:21 AM
int c = 0;
if(conidtion1){
statement;
stetement;
if(condition2){
c = 1;
statement;
}
}
if(c==1) statement; // again wont happen if condition2 is not met



Surely using a flag such as above would be far cleaner then using the goto statement. Really it would be more sensible to use seperate functions here but you get the idea. There is always an alternative to using goto and generally it is a good idea to use it unless you are being lazy.
Jeffrey Palermo wrote re: The condemned "goto x;" jump statement - level 200
on Wed, Apr 13 2005 5:28 AM
<i>Show me one, ONE MS guy who says that he can see a valid use for you using it in production code.
</i>
I found, ironically enough, in a Microsoft example (VS.NET 2003)
<program files>\Microsoft Visual Studio .NET 2003\SDK\v1.1\Samples\Technologies\Interop\Applications\CDO\InboxViewer\CDO.cs

Line 157:
catch (Exception e) {
MessageBox.Show("Fatal Error: " + e.ToString());
goto done;
}

Brian Lowry wrote re: The condemned "goto x;" jump statement - level 200
on Fri, Oct 12 2007 1:14 PM

Goto statements are the devil. I used them in 4th grade when I started learning BASIC. Haven't we evolved?

Steve wrote re: The condemned "goto x;" jump statement - level 200
on Tue, Oct 16 2007 11:18 AM

GOTO is great feature. I use it every time I can.

It makes your code very fast and there are other advantages like "code monopoly".

So you are the only one who can develop the code further.

I realy like it.

Devlicio.us