GoTo: is it really evil?

Started by
151 comments, last by Troll 18 years, 1 month ago
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~
Advertisement
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
Nowadays, pretty much the only use I would consider good would be breaking out of multiple levels of loops. Pretty much every other use can be replaced by exceptions, loops or functions. As for your question, you cannot goto a label outside of the current function.
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++.
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.
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?
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.
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
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
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++.

This topic is closed to new replies.

Advertisement