Goto.

So, I've been fooling around in C++ for maybe 2-3 weeks now and the book that I'm using calls goto "dreaded" or "horrendous". Why is that? It seems to me that it would have some decent functionality if at the expense of making your code look cluttered. Why is goto reviled so much?

It breaks control flow.
It reduces readability of programs (no longer top-to-bottom logic).
There aren't any good uses that don't have better alternatives.
Google will tell you more.

Fun Fact:
As a quirk of syntax, this compiles:
http://www.google.com/

http://www.google.com/
"http:" is a label, and the rest is a comment ;)
Last edited on
closed account (z05DSL3A)
There is nothing wrong with goto if used correctly, like most things it can be abused/used incorrectly but that is C++ for you. Learn how to use it and not abuse it. Then, if the need arises, you will be armed to address the problem be it a more elegant solution using it or recognizing when someone has messed up with it.

Read more here: http://www.stevemcconnell.com/ccgoto.htm
Personally, I've never felt the need to use goto's. However, I do feel I sometimes use too many if->returns to hardbreak out of loops. I wonder if that's any better than goto-ing.
Wasn't Label:... goto: deprecated long ago? So why use it?

I used to use goto like you, then I took an arrow to the knee :)

Wazzak
The nature of goto is that it's unconditional. Jumping forward or backward. Both don't make sense

if you have something like if(x > 0) goto x_greater_0;, well, why not writing the appropriate code within the if clause?

if you're tempted to use goto to get out of the mess then you really should redesign you program.

Here's a funny little example:
1
2
3
goto a;
int x = 0;
a: int y = x + 1; // y = ??? 
I think that goto is in the language just because when C was initially written this kind of programming thinking was quite popular. The world has moved on since then though and nowadays not only there is no need to use it but it's use can easily become dangerous.

Just don't use it. It violates a lot of the programming thinking you are trying to achieve programming in C/C++ like structured programming. Don't be tempted to use it. Try to find a less error prone alternative. Sure there is one.
Last edited on
Google: Spaghetti Code.
closed account (z05DSL3A)
Wasn't Label:... goto: deprecated long ago?
No, not that I have seen anywhere. I believe that they have tightened up the rules for goto in the new standard, not the sort of thing you do with deprecated features.

So why use it?
I'm not saying use it or don't use it, I'm saying understand it and don't be scared of it because someone said 'it's evil'.

Edit:

coder777 wrote:
Here's a funny little example:
1
2
3
goto a;
int x = 0;
a: int y = x + 1; // y = ???  

...and here are the funny little warnings you get:
warning C4702: unreachable code
warning C4700: uninitialized local variable 'x' used

;0)

Last edited on
Thanks for the clarification. That makes quite a bit more sense now. Especially your entertaining little example grey wolf.

I think it is good to avoid goto to begin with. It is almost always the wrong solution to the problem. It is because it is so easy to misuse that books warn beginners away from it. Much evil can indeed be done with goto. But it does have legitimate uses, so I wouldn't write it off entirely. Just forget about it for the next 10 years or so ;o)
Galik wrote:
I think it is good to avoid goto to begin with. It is almost always the wrong solution to the problem. It is because it is so easy to misuse that books warn beginners away from it. Much evil can indeed be done with goto. But it does have legitimate uses, so I wouldn't write it off entirely. Just forget about it for the next 10 years or so ;o)


This. There are times when goto is actually more clear than the alternatives (but they are rare). For example, they can break out of nested loops in one shot without cluttering all sorts of boolean exit conditions all over them.
Topic archived. No new replies allowed.