Goto Command is bad?

Pages: 123
I have seen several statements that this command is basically a bad thing to use, but nobody ever really states why. I saw looked it up on google, but the answer was philosophical, and not a technical answer so I am asking here. I don't want to start using it and regret it later.
closed account (10oTURfi)
Well, in the end, compiler will turn all loops and gotos into jump instructions, so it "doesnt matter" performance wise. But it creates "Spaghetti code" which is hard to read therfore making it "bad habbit".

Some problems may occur (in theory) when using goto. Such as dynamically allocated data no longer exist.

goto can be actually very usefull when you want to break from deeply nested loop.
Last edited on
I've never seen an example where goto is absolutely required. As Krofna stated, it makes for very messy and unreadable/undebugable code.

These statements interrupt the flow of the code when you are reading it and so really the only reasons to avoid them are:
1. goto makes it much more difficult to debug since you don't know how that particular code was accessed and what happened first.
2. anybody else who may want to read your code will need to spend way more time studying it to figure out the structure. They will want to kill you.

Just try and make a complicated project with multiple gotos. When you succeed, tell us if you had any problems debugging, or re-reading the code when you picked up again in the mornings. You'll find that by skipping goto things will be much simpler and you'll be able to construct more complicated projects easily.
If you use logic and control structures correctly, you never need to use them.

NEVER use goto. It is pure evil and everyone will hate you for it. NEVER! I mean it.
NEVER!!
closed account (1vRz3TCk)
See:
Code Complete: A Practical Handbook of Software Construction.
http://www.stevemcconnell.com/ccgoto.htm

Learn how to use goto and use it where appropriate (usually when there isn't a better way).

?? What do you mean, CodeMonkey? Are you implying that there IS an appropriate use? I bet most of us will just say NO!, there is not a single case in the whole world where Goto is the only way to go, or even the better way to go.

And no, I won't read that. :P
I used to work on embedded code controlling safety-of-life systems; actions to be taken in the event of fire, for example. When the requirements were reached to take safety-of-life action, we used a goto; stepping up several levels of embedded loop, or calling a separate function requiring new stack frames etc., was deemed to take too long and involved unnecessary risks - nothing mattered more than doing the necessary action immediately.
Last edited on
closed account (1vRz3TCk)
webJose wrote:
Are you implying that there IS an appropriate use?

Yes.
I bet most of us will just say NO!, there is not a single case in the whole world where Goto is the only way to go, or even the better way to go.
I would probably agree...that most of 'us' would say no.
Sorry, but I don't buy it. Are you saying that an additional frame in the stack is life-threatening? Maybe in an 80's PC, but with today's power I just don't buy that argument. Any hard facts supporting the apparent extreme need of speed?
Are you saying that an additional frame in the stack is life-threatening?


Yes. That was the decision made. Something somewhere has gone very very badly wrong. One thing needs to happen. Everythign between here and there is an opportunity for something to go so wrong that we never reach the critical code. For all we know the lump of memory the next stack frame will be put in doesn't even exist anymore.

I do not at all apologise if this offends your sense of coding elegance.
Wow. Too bad there's no documentation on the fact, because it is way too hard to digest like that! I mean, I feel like I've just been told that 1 + 1 is not always 2! Really, like that.

BTW, it DOES offend my sense of coding elegance for sure! But hey, it's not your fault, I guess so don't feel like you need to apologize.
One more thing: Do you know assembly? I'd love to see a post here showing the assembly of a goto and the assembly of a function code.

BTW, I just thought about it: Function inlining. Doesn't that solve the frame problem? I bet it does, and the nasty goto can go to hell!
Your lack of imagination is not surprising. You speak like some kind of religious fanatic; clearly, you do not support you position with reason, so you cannot listen to reasonable argument.

I suggest we end this here; people continue to put goto into code for good reason and will continue to do so for a long time.
LOL!!!! So asking for hard proof and assembly code for the two methods (a goto and a function call) is being unreasonable and being a person that lacks imagination??? Dude, just say: Yes! You got me there. Function inlining removes the need of the goto in life-threatening situations.

Be a man. Admitting that you're wrong will only make you stronger. :D
closed account (1vRz3TCk)
I mean, I feel like I've just been told that 1 + 1 is not always 2! Really, like that.

The same can be said for your assertion that there is always a better way and got should never be used. Would you care to offer proof for that?

The cases where goto would be more appropriate would require a more complicated problem than the sort used in forums so the likelihood of anyone volunteering one would be small. I have used goto a number of times (with strict rules on its use).

Edit:
JOINT STRIKE FIGHTER AIR VEHICLE C++ CODING STANDARDS
AV Rule 189 (MISRA Rule 56)
The goto statement shall not be used.
Rationale: Frequent use of the goto statement tends to lead to code that is both difficult to read and maintain.
Exception: A goto may be used to break out of multiple nested loops provided the alternative would obscure or otherwise significantly complicate the control logic.
Last edited on
Hello CodeMonkey. Thank you for the question. Since the set of all possible scenarios is one with potentially infinite scenarios, it is really impossible for anyone to provide a satisfactory proof of this.

However, most expert programmers will take on the challenge of converting any code using Goto into code that doesn't and is equivalent. And nowadays, college teachers strongly discourage the use of goto, meaning they agree with this.

So hard proof that all goto scenarios can be converted? No. But if you have one, feel free to post it. We'll analyze it.

AND TO WHOMEVER REPORTED MY POST: I'm like the honeybadger. :-) I don't care if I live or die in this forum. Report all you want. Just make sure you are VERY good at the logic of why the above post is offensive. Wouldn't anyone of you agree that me being compared to a religious fanatic is worse?? Think about it.
Last edited on
Hey, I just saw your edit. I see the exception there. I can clearly see where that is coming from for sure. But I bet you I can make it clear. From any number of loops. 50, 100, 1000. Maybe not in EVERY language, but in C++ using templates I can provide a generic loop-and-exit framework that you can embed with ease. Never done it, but my brain has quickly scanned the preliminary idea and is giving me the thumbs up! And for a guy that LACKS IMAGINATION I think that's a good sign, right? :D
closed account (1vRz3TCk)
TBH I don't think it is a good sign they you dismiss the use of goto.
Pages: 123