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

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
Movie: Priest Academy Movie: Priest Academy
Inspirational Rat Story Inspirational Rat Story
A face-melting DSL that allows programming ON the iPhone (and iPad) A face-melting DSL that allows programming ON the iPhone (and iPad)
The secretGeek Disaster Recovery plan The secretGeek Disaster Recovery plan
Save KNVTn! Before it's too late Save KNVTn! Before it's too late
The Ultimate Agent of WERF Destruction The Ultimate Agent of WERF Destruction
The new prisoner's dilemma The new prisoner's dilemma
Original Premise for a road movie Original Premise for a road movie
What's a better game than Devshop? What's a better game than Devshop?
DevShop: The Cool Game that Makes Development Look Fun DevShop: The Cool Game that Makes Development Look Fun
Should be purple Should be purple
Kitchen Agile Kitchen Agile
Perhaps Perhaps "Go" is the new Visual Basic
zen-coding: turn those CSS selectors upside down zen-coding: turn those CSS selectors upside down
Debugging: It's all about finding Albuquerque. Debugging: It's all about finding Albuquerque.
The Real-Time online JQuery Editor The Real-Time online JQuery Editor
HTML5, a 3 minute guide HTML5, a 3 minute guide
Developer Codpieces Developer Codpieces
Agile for one: The Personal Story 'Wall' In Action Agile for one: The Personal Story 'Wall' In Action
Never work with thick people. Never work with thick people.
Cosmo: project status panel Cosmo: project status panel
Windows Search in Japan Windows Search in Japan
Project Management Zen Project Management Zen
Continuous Integration, Plugins and Going Too Far Continuous Integration, Plugins and Going Too Far
The Rules of Stand Up The Rules of Stand Up
Sydney International Airport: Stupid, Criminal, or Criminally Stupid? Sydney International Airport: Stupid, Criminal, or Criminally Stupid?
God No! ...The ReBuilder God No! ...The ReBuilder
Matt, The Office Mortar Matt, The Office Mortar
'Outlook style' rules for Subversion 'Outlook style' rules for Subversion
Really deep linking: Url + regex Really deep linking: Url + regex
hExcel -- A Hexagonal Spreadsheet hExcel -- A Hexagonal Spreadsheet
Is the remote control a thing of the past? Is the remote control a thing of the past?
The Utterly Thorough Guide To Awesome Application Compatibility on Windows 7. The Utterly Thorough Guide To Awesome Application Compatibility on Windows 7.
Astounding Hyperlinked Noticeboard Astounding Hyperlinked Noticeboard
Three Questions About Each Bug You Find Three Questions About Each Bug You Find
Recursing over the Pareto Principle... Recursing over the Pareto Principle...
Sometimes, The Better You Program, The Worse You Communicate. Sometimes, The Better You Program, The Worse You Communicate.

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

World's Simplest Code Generator (html edition) World's Simplest Code Generator

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 Best Software Writing I
The Business Of Software (Eric Sink)

Recommended blogs

Jeff Atwood
Reginald Braithwaite
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Rhys Parry
Joel Pobar
OJ Reeves
Eric Sink
Joel Spolsky
Des Traynor

Aggregated Links

programming.reddit.com
dzone
dot net kicks

Human Link Machines

interesting finds
a continuous learner's weblog
arjan's world
n links today
new and notable
morning coffee
learning .net
weekly link post
(my del.icio.us account)

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

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