Skip to main content
GameDev.net gamedev.net
Using GameDev.net for your class this semester?
Learn more →
🔒 Locked

GoTo: is it really evil?

Started by Servant of the Lord Mar 10, 2006 at 6:23 PM 151 replies 29.5k views
Original Post
Servant of the Lord
Servant of the Lord
When I learned of the Goto statement the book I learned it from told me it was 'improper code' and 'error causing' also mentioning that 'the pros don't use this command'. I thought; why not? So I popped up wikipedia and read on it. The only thing it mentions about it being wrong, besides downgrading it and again mentioning that advanced programmers avoid it, was that it leads to sloppy/unreadable code. Is that all there is to it? All the 'goto is evil' is just references to its sloppyness? Is there any actaul reason I shouldn't use the goto statement if I explain well where my labels are? Oh, and can goto work through to other files in a project; and what must I do to make it work, I.E. 'extern label LabelName;'? Thanks. ~S of the L~
Anon Mike
Anon Mike
No it's not "evil". Used judiciously it can make your code easier to read and more efficient.

The whole "goto is evil" thing came from a time before every language under the sun had higher level loop constructs or even stuff like functions. In such an evironment code tended to become a mass of hard to follow gotos going every which way.

That being said, it's very rare that you need a goto. Pretty much the only time you see them anymore is for quick exits in error conditions. And even that is starting to dissappear as people move more towards exception based models.
-Mike
SiCrane
SiCrane
Goto often represents a failure to maintain a proper level of semantic information in your code. Replacements for goto, such as for and while loops, should be prefered as they convey greater information than a goto/label pair, which makes them easier to understand for humans and often subject to better optimization by compilers.

Goto cannot be used to jump to code into other files. You may want to consider setjmp()/longjmp() if you are programming in C or C++.
Servant of the Lord
Servant of the Lord
Thanks guys; those answered my questions perfectly. I have no need of using gotos anyway, just though they might make my coding easier. Guess if eveyone is moving away from goto I shouldn't move toward it. Thanks.
Servant of the Lord
Servant of the Lord
Quote:
Original post by SiCrane
Goto cannot be used to jump to code into other files. You may want to consider setjmp()/longjmp() if you are programming in C or C++.


Neverheard of those 'jumps' I will check wikipedia on them; do you know of any good sites on them?
medevilenemy
medevilenemy
In languages like BASIC and Visual BASIC, gotos (of various kinds) are quite good to use...

With C/C++ there is no real use for a goto that couldnt be served better by a seperate function. Trust me, all of my earlier programs used many (MANY MANY MANY) goto statements, and after a while the code was sort of difficult to read, but worse is that is was more difficult to debug. Anyway, the goto statement itself is ment to set aside a section of code, effectively to be run by proxy. Why not do this with a function? Functions are much cleaner. And you CAN use functions across files (so long as you include the appropriate file into the main source).
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
M2tM
M2tM
I haven't used a goto in years. Quite literally -years-.

The last time I used a goto was in qbasic in grade 7 and oh god did I ever. I used gotos combined with flags to create what I now know could have been done with subfunctions or gosubs. It was horrible.

Basically, do not utilize gotos in your code unless there is no graceful alternative, and even then, make sure that what you are trying to do is correct in solving the real problem. Chances are if you find yourself with a goto in your code, there's been a bungle somewhere.
_____"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
Conner McCloud
Conner McCloud
Quote:
Original post by M2tM
The last time I used a goto was in qbasic in grade 7 and oh god did I ever. I used gotos combined with flags to create what I now know could have been done with subfunctions or gosubs. It was horrible.

That's not so bad. I used goto to implement ELSE and all the loops in my first qbasic program [grin] I was basically programming in assembly. Which, in retrospect, probably helped quite a bit when I actually learned assembly. So there you go, goto is a wonderfully useful construct.

CM
Roboguy
Roboguy
Quote:
Original post by Servant of the Lord
Quote:
Original post by SiCrane
Goto cannot be used to jump to code into other files. You may want to consider setjmp()/longjmp() if you are programming in C or C++.


Neverheard of those 'jumps' I will check wikipedia on them; do you know of any good sites on them?


You don't want to use these unless there is no alternative. There is also a similar (although somewhat more powerful) concept ("continuations"), although it is not easily usable in C or C++.
M2tM
M2tM
Quote:
Original post by Conner McCloud
Quote:
Original post by M2tM
The last time I used a goto was in qbasic in grade 7 and oh god did I ever. I used gotos combined with flags to create what I now know could have been done with subfunctions or gosubs. It was horrible.

That's not so bad. I used goto to implement ELSE and all the loops in my first qbasic program [grin] I was basically programming in assembly. Which, in retrospect, probably helped quite a bit when I actually learned assembly. So there you go, goto is a wonderfully useful construct.

CM


Heh, yeah, I've done it before. My first major program, thankfully, used looping and decision structures ;)

I'm thinking of posting a thread when I get home "Your first game" where you can post the first game you made and it's source if you want. I've got a Barny blaster that hasn't been seen by anyone but a handful of people, and I figure there are some other first projects out there that might bring back memories ;)
_____"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
chaosgame
chaosgame
I use GOTO every time I program my TI-83+ graphing calculator. The alternative is creating a new program for each subroutine which makes the program menu very messy. There are no built-in subroutines and functions.
"Are you threatening me, Master Jedi?" - Chancellor Palpatine
Anon Mike
Anon Mike
You're not a real man until you've written a computed goto in assembly that hot-patched the instruction stream with the goto target!
-Mike
Will F
Will F
Quote:
Original post by Servant of the Lord
So I popped up wikipedia and read on it.


Check out the entry for spaghetti code. There's also the seminal essay, Go To Statement Considered Harmful.

Quote:
The only thing it mentions about it being wrong, besides downgrading it and again mentioning that advanced programmers avoid it, was that it leads to sloppy/unreadable code.


In langauges like C/C++ there is usually a better solution to a problem than using goto. Would you rather write/maintain well structured C/C++ or something that looks like this Atari 2600 assembly code (though I must say that the hidden message in the code to Adventure is pretty neat)?
hplus0603
hplus0603
Quote:
And even that is starting to dissappear as people move more towards exception based models.


Yeah, exceptions are so much easier to read than gotos. It's not like exceptions are just a big goto that you can't figure out where it'll end up until at runtime, or anything.

For the doubting: yes, I'm being facetious.
enum Bool { True, False, FileNotFound };
markr
markr
In my opinion, there is no reasonable reason for using goto in C++.

Any valid uses of "goto" which existed, are now covered by exception handling.

It just exists for legacy compatibility reasons.

For the OP: You cannot use goto between different compilation units, nor can you use goto to labels outside of the current function (or maybe lexical block).

I am aware of the usage of "goto" in Linux, however,

a) That is written in C, not C++ (For reasons which Linus Torvalds has explained clearly on numerous occasions), hence can't use exceptions
b) It is used at least 90% of the time for reusing cleanup code in error conditions - this is a reasonable use.
c) It is written by people who know what they're doing :)

Mark
jpetrie
jpetrie
Quote:

Any valid uses of "goto" which existed, are now covered by exception handling.


Not really true, because exceptions should not be used for flow control. Throwing an exception to jump out of a deeply nested loop (one of the few remaining remotely reasonable reasons for a goto) is far more disgusting than the goto itself.

EDIT: to clarify, I'm not really condoning this particular use of goto, because Shannon's post, below, is definately correct. I'm merely trying to state that throw an exception to break out of a loop is bad.

[Edited by - jpetrie on March 10, 2006 10:27:12 PM]
Shannon Barber
Shannon Barber
If you're using goto's to break out of nested loops, it is a certain sign that you need to break the code into sub-function.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.