GoTo: is it really evil?

Started by
151 comments, last by Troll 18 years, 1 month ago
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
Advertisement
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
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
As far as I'm concerned, GOTO does not exist in C++!
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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)?
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 };
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
<voice family="old man">Oh you young whipper snappers and your fancy GOTO statements! In my day, we only had COMEFROM!</voice>
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]
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

This topic is closed to new replies.

Advertisement