You Learn Something (Scary) Every Day
secretGeek .:dot Nuts about dot Net:.
home .: about .: sign up .: sitemap .: secretGeek RSS

You Learn Something (Scary) Every Day

Well it turns out you can actually use 'goto' statements in C# !!

    private void button1_Click(object sender, System.EventArgs e)

    {

        goto done;

        MessageBox.Show("Bet this doesn't get shown!");

        done: return;

    }

Conversation overheard in the next cubicle:

I didn't realise that VB.net still has 'goto' statements. Scary.

Want to know something even scarier?

(pause for a beat.)

C# has a goto statement too.

Really!?

Yeh, but no one ever uses it.

Why not?

Cause you'd be shot. That's why.





'CGamesPlay' on Wed, 05 Jul 2006 10:37:36 GMT, sez:

Well, I'm having trouble deducing the attitude of the author towards gotos, but that is a neat trick.



'Groucho' on Wed, 05 Jul 2006 14:35:52 GMT, sez:

There is a time when you might need to use the goto statement because of the handling of switch statements. The compiler won't let you fall-through from one case to the other like from case 2 to case 3 below.

switch(i) {
case 1:
case 2:
/* some code */
case 3:
/* some code */
break;
default:
break;
}

It will allow case 1 to case 2 as far as I know because case 1 has no code (like an OR).
But the compiler will force you to have a break at the end of case 2, unless you put a goto, return, or throw.
So if you actually want it to fall through, you would need:

switch(i) {
case 1:
case 2:
/* some code */
goto case 3;
case 3:
/* some code */
break;
default:
break;
}



'lb' on Wed, 05 Jul 2006 21:02:02 GMT, sez:

yeh i've seen this reason given.

is this a bit of a big hammer for a small nail?



'marty' on Wed, 05 Jul 2006 21:56:38 GMT, sez:

I thought the whole "GOTO is bad!!!!!" thing died years ago, but it always comes back :)



'Groucho' on Wed, 05 Jul 2006 22:22:12 GMT, sez:

@lb
I certainly think so. Apart from their use in switch statements (I have issues with how switch statements work in .NET so that's arguable at best), I don't see a reason for them. That hazards simply outweigh the benefits, and you can always accomplish the same thing with the standard control flow statements. if,while,for, etc. are there so we don't need goto's after all.
Interestingly, I see them pop up in disassemblies of .NET exe/dll's more often than I would have expected. They replace continue statements in loop blocks for instance:
while(true) {
...
continue;
}

becomes

Label_0332:
while(true) {
...
goto Label_0332;
}



'Edsger W. Dijkstra' on Wed, 05 Jul 2006 22:25:26 GMT, sez:

For a number of years I have been familiar with the observation that the quality of programmers is a decreasing function of the density of go to statements in the programs they produce.

It seems that this C# compiler is a programme of the lowest quality.



'lb' on Thu, 06 Jul 2006 00:15:13 GMT, sez:

so under the cover (in IL i mean) are all of these 'GOTO'/Jump statements:

break
continue
goto
return
throw

??



'Groucho' on Thu, 06 Jul 2006 14:19:36 GMT, sez:

I know very little about the process so I couldn't say. The code ends up being re-arranged quite a bit because of the optimization the compiler goes through, so it would all depend on the code. But, I tested it out quickly. I only saw the continue statement being replaced with a goto, and only in a while loop, not for loops (having it jump to just before the loop like in the while loop wouldn't make any sense). Instead, the code gets re-arranged to remove the continue statement. No continue's in IL apparently.



'http://' on Mon, 10 Jul 2006 15:49:27 GMT, sez:

There are reasons for using a GOTO, even in C#. However, only *very* experienced programmers are going to know when it is appropriate and when it is just a crutch. Because of that, I'm kind of glad people haven't noticed that it exists.

With C# there are enough control structures there is never a case that a GOTO can't be worked another way. Perhaps the avoidance of GOTO adds perhaps a bit of overhead: but at this point the minor amount of overhead is rarely a problem in the real world.

If you want to see excellent examples of GOTO used properly, find a copy of the old TeX code by Donald Knuth. Considering he wrote the book on programming (literally: see the "Art of Computer Programming") it shows it can be used instead of abused. Sadly, he is also one of the best programmers ever to exist: most programmers simply abuse it, thus the prohibition.



'Zooba' on Wed, 12 Jul 2006 03:02:35 GMT, sez:

Most likely (I'm not extremely well-versed here) IL is based on similar constructs to assembly language, which (as is always taught at universities) 'only' has GOTO. It also has RETURN, so to look at the HLL statements and their equivalent low-level ones:

BREAK - go to next closing brace
CONTINUE - go to last opening brace
GOTO - go to a place which has a name
RETURN - go to address on top of stack
THROW - set a global variable then RETURN

Because of the architecture of the x86, GOTO equivalents (jumps) are unavoidable, and everything has to resolve to a GOTO at some point, otherwise the code runs straight from top to bottom. There are certainly times when using a GOTO is clearer than the equivalent 'structured' construct, though any more than a whisper about it is enough to bring the HLL-purists down on you.



'lb' on Wed, 12 Jul 2006 04:31:23 GMT, sez:

cheers Zooba -- good points.

i use goto statements in batch files a lot. i think they can be very neat so long as you're very very consistent with them.

actually the real challenge is having consistent and meaninful label names.

from the little i remember about assembly language isn't there JMP (that can jump anywhere) and JMP IMMED (branches maybe??) that can't jump as far? oh i'll shut up now, i remember nothing.

cheers
lb



'Zooba' on Wed, 12 Jul 2006 23:13:48 GMT, sez:

That's true. However, generally the assembler will decide which one to use. Modern assemblers have high level constructs as well (see http://www.masm32.com/).

Generally conditional jumps (based on the result of a previous comparison) can't go as far as an unconditional one, but that's just the instruction set.



'lb' on Thu, 13 Jul 2006 04:04:59 GMT, sez:

hey zooba -- email me if you want (leon bambrick at gmail dot com, all one word)-- i wanna talk about your goto method (http://www.byteclub.net/blog/zooba/?p=23)



'just a visitor' on Wed, 02 Aug 2006 00:03:46 GMT, sez:

Actually, goto can be useful if you translate from a language that has them (example: fortran) and if this language has a huge legacy library (example: netlib for many numerical computations) and you have a way to tranform the programs automatically(example: using f2c).



[Comments closed due to spam]

Articles

Mind-boggling Demo of New Gaming Genre, aka Folder-Based Hangman, aka Fun with Recursion Mind-boggling Demo of New Gaming Genre, aka Folder-Based Hangman, aka Fun with Recursion
Got CSV in your javascript? Use agnes. Got CSV in your javascript? Use agnes.
I went to write down a book name and founded an internet empire instead. I went to write down a book name and founded an internet empire instead.
NimbleText: Origins NimbleText: Origins
The Windows 8 Mullet The Windows 8 Mullet
Cosby: spontaneous striped background generator Cosby: spontaneous striped background generator
Slides from WDCNZ: Live Coding Asp.net MVC3 Slides from WDCNZ: Live Coding Asp.net MVC3
MVC 3, MVC 3, "Third Times a Charm" references
Custom Errors in ASP.Net MVC: It couldn't be simpler, right? Custom Errors in ASP.Net MVC: It couldn't be simpler, right?
Anatomy of a Domain Hijacking, part 2: The Website Who Came In From The Cold Anatomy of a Domain Hijacking, part 2: The Website Who Came In From The Cold
Anatomy of a Domain Hijacking, part 1 Anatomy of a Domain Hijacking, part 1
secretGeek.net domain has been stolen. The site may go down. secretGeek.net domain has been stolen. The site may go down.
Boring article: 'untrusted domain' issue with SQL Server. Boring article: 'untrusted domain' issue with SQL Server.
Coding While You Commute Coding While You Commute
Test Driven Dentistry Is A Good Thing Test Driven Dentistry Is A Good Thing
The 'less crashy' release of NimbleText The 'less crashy' release of NimbleText
Rethinking Toolbars in Visual Studio (or any IDE) Rethinking Toolbars in Visual Studio (or any IDE)
Where shall we have lunch? Where shall we have lunch?
Setting up email for your microIsv Setting up email for your microIsv
The NO Visual Studio movement: Compiling .net projects in Notepad++ The NO Visual Studio movement: Compiling .net projects in Notepad++
ZeroOne: the editor for programmers who think in binary ZeroOne: the editor for programmers who think in binary
Mercurial workflow for personal projects (with a .net bias) Mercurial workflow for personal projects (with a .net bias)
I see you're using vim. Let me fix that for you. I see you're using vim. Let me fix that for you.
The worst recruitment spam I've ever read The worst recruitment spam I've ever read
A thank you I forgot to say A thank you I forgot to say
My new product, NimbleText, is live My new product, NimbleText, is live
Grabbing the free songs of Jonathan Coulton (with Powershell) Grabbing the free songs of Jonathan Coulton (with Powershell)
Using NimbleSet to compare lists Using NimbleSet to compare lists
Wanted: Wiki Lists (dot org) Wanted: Wiki Lists (dot org)
DOS on Dope: The last MVC web framework you'll ever need DOS on Dope: The last MVC web framework you'll ever need
JSON Query Languages: 5 special purpose editors JSON Query Languages: 5 special purpose editors
What then, is b? What then, is b?
SQLike: A simple editor SQLike: A simple editor
Yet Another BizPlan Generator. Yet Another BizPlan Generator.
HOT GUIDS: A hot or not site for guids HOT GUIDS: A hot or not site for guids
How does life get better? One tiny hack at a time. How does life get better? One tiny hack at a time.
24 things to do, and 100 things *not* to do (yet) for building a MicroISV 24 things to do, and 100 things *not* to do (yet) for building a MicroISV
Venture capital won't kill Jeff Atwood, it will only make him Jeffer. Venture capital won't kill Jeff Atwood, it will only make him Jeffer.
A handy workflow image for newbie mercurial users A handy workflow image for newbie mercurial users
Fractal Feedback, a diversion into recreational programming Fractal Feedback, a diversion into recreational programming
Hump-Jumping: How the Education of Computer Science can be Saved, err, maybe. Hump-Jumping: How the Education of Computer Science can be Saved, err, maybe.
Suggested User Experience Improvements for DiffMerge Suggested User Experience Improvements for DiffMerge
SQL Style Extensions for C# SQL Style Extensions for C#
The Movie Hollywood (And My Wife) Doesn't Want You To See: Weekend at Jacko's The Movie Hollywood (And My Wife) Doesn't Want You To See: Weekend at Jacko's
Sysi: the ultimate administrators toolkit Sysi: the ultimate administrators toolkit

Archives .: secretGeek :: Complete Archives
TimeSnapper -- Automated Screenshot Journal TimeSnapper.com    
Version 3.3: true productivity boost

Next Action NextAction
Managing the top of your mind

NimbleText -- World's Simplest Code GeneratorNimbleText -- World's Simplest Code Generator, Text Manipulator, Data Extractor

25 steps for building a Micro-ISV 25 steps for building a Micro-ISV
3 minute guides -- babysteps in new technologies: powershell, JSON, watir, F# 3 Minute Guide Series
Universal Troubleshooting checklist Universal Troubleshooting Checklist
Top 10 SecretGeek articles Top 10 SecretGeek articles
ShinyPower (help with Powershell) ShinyPower
Now at CodePlex

Realtime CSS Editor, in a browser RealTime Online CSS Editor
Gradient Maker -- a tool for making background images that blend from one colour to another. Forget photoshop, this is the bomb. Gradient Maker


[powered by Google] 


How to be depressed How to be depressed
You are not inadequate.



Recommended Reading


the little schemer


The Best Software Writing I
The Business Of Software (Eric Sink)

Recommended blogs

Jeff Atwood
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Rhys Parry
Joel Pobar
Thomas White
OJ Reeves
Eric Sink

Aggregated Links

proggit
dzone
hacker news
dot net kicks

Human Link Machines

interesting finds
a continuous learner's weblog
arjan's world
weekly link post

LinkedIn profile
LogEnvy - event logs made sexy
Computer, Unlocked. A rapid computer customization resource
PC Smart Buys - Computer Hardware in Australia
 
home .: about .: sign up .: sitemap .: secretGeek RSS .: © Leon Bambrick 2006 .: privacy

home .: about .: sign up .: sitemap .: RSS .: © Leon Bambrick 2006 .: privacy