Skip to main content
GameDev.net gamedev.net
🔒 Locked

Why is using goto so bad?

Started by MrPoopypants Dec 17, 2002 at 4:06 PM 151 replies 7.6k views
Original Post
MrPoopypants
MrPoopypants
Hi guys I was working on one of my C++ assignments, desperately trying to find a quick way to get out of a nested loop when i recalled my old Ti-83 programming days when i would use GOTO a lot. I used this in my C++ project, and the teacher''s responce was: "we DO NOT use goto''s in this class." I thought it was a bit of an overreaction but he said it promoted bad programming practice and screws up debugging and went on to say it was from the original FORTRAN compiler and he doesnt know why the kept it. I think goto''s are wonderful, but what do you guys think/know about them? Thanks
(0110101101000110)The Murphy Philosophy: Smile . . . tomorrow will be worse.
prh99
prh99
quote:
Original post by MrPoopypants
Hi guys
I was working on one of my C++ assignments, desperately trying to find a quick way to get out of a nested loop when i recalled my old Ti-83 programming days when i would use GOTO a lot. I used this in my C++ project, and the teacher''s responce was: "we DO NOT use goto''s in this class." I thought it was a bit of an overreaction but he said it promoted bad programming practice and screws up debugging and went on to say it was from the original FORTRAN compiler and he doesnt know why the kept it.
I think goto''s are wonderful, but what do you guys think/know about them?
Thanks




Your instructor is right, the Goto statement promotes bad programming habits. Heaven forbid you even have to debug reasonably large program that contains Goto''s, you have track down all the targets. If you get hired as a programmer chances are good other people will have to read your code and Goto statement turn you code into a rats nest.
Patrick
Raduprv
Raduprv
I used goto a LOT when programming in ASM (since you can''t do anything usefull in ASM without ''goto'' (jmp, jz, jnz, etc.)). When i switched to PHP, for a few weeks, I thought I am goign crazy, when I found out that there is no goto in PHP. Then, I got used to it. When i switched to C, I had no use for goto, and I do find it a thing to be avoided (but nto a FORBIDDEN thing). Basically, you can do anythign without a goto, so...

Height Map EditorEternal lands
SabreMan
SabreMan
quote:
Original post by Anonymous Poster
if you''re in 10 nested loops and need to go out 5, goto is the best option.

If you''re in 10 nested loops, then you''re beyond help.
AndyOxfeld
AndyOxfeld
I personally code in a way that I don''t need goto. But as long as it''s easy to understand what you''re doing, and you only use it when there''s no better way, then yeah, ignore your instructor and use goto anyways.

- Andy Oxfeld
Naaga
Naaga
I think this is another pointless religious debate. It''s just that 98% of programmers are on the same side. I think goto is useful in certain places. The problem is over use. I wouldn''t use more than one goto in a single function, and it would be clearly marked with comments. I would only use it to make the code cleaner and more readable.
__________America seems to like crap because its what we make popular. - Goober King
DrPizza
DrPizza
quote:
Original post by SabreMan
If you''re in 10 nested loops, then you''re beyond help.

Unless you''re using a powerloop.


char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
MaximuS_X
MaximuS_X
The way languages are designed today, the GOTO becomes obsolete. Any loop that is made with a GOTO can be made using IF,WHILE,FOR and DO statements. That is why these statements exist, they are substitutes for GOTO's. The only time GOTOs are useful is at the assembly level (Branches and jumps). Dijkstra wrote a good paper on it.

[edited by - MaximuS_X on December 17, 2002 5:40:24 PM]
---
Balron
Balron
I only use goto when adding conditions to each of a set of nested loops makes it too confusing to be reasnoble.

- Your Local Drunk
Your Local Drunk
Sneftel
Sneftel
quote:
Original post by MaximuS_X
The way languages are designed today, the GOTO becomes obsolete. Any loop that is made with a GOTO can be made using IF,WHILE,FOR and DO statements. That is why these statements exist, they are substitutes for GOTO''s.


True. goto statements aren''t necessary. Neither are break statements, continue statements, for loops, or using "return" anywhere other than as the last line of a function. But all of these breaks from "pure" procedural programming are very useful, if not absolutely necessary.


Don''t listen to me. I''ve had too much coffee.
Beer Hunter
Beer Hunter
Goto is only bad when used in place of a more readable construct. However, this turns out to be most of the time. For this reason, one should understand as many language constructs as one can before resorting to goto.
Wreakon
Wreakon
I feel that goto's are a feature of the language like if and while, but goto's should NOT be used for things that can be more obviously done, although I use it in instances like this piece of code:


if ( ERROR != DOSOMETHING )
{
Do something..
}
else
{
DeleteHandle(something);
delete Object;
return ERROR;
}

Do something in between...

if ( ERROR != DOSOMETHING#2 )
{
Do something...
}
else
{
DeleteHandle(something);
delete Object;
return ERROR;
}

At some points if statements like this pile up in a function, and that stuff in the else statement becomes overused. Doing this is now not feasible because we have to do something between the two statements, and unless we write another function it can't work.

//Wont work
if ( ERROR != DOSOMETHING &&
ERROR != DOSOMETHING#2)

Although there may be a way of doing it without a goto (I just can't think of one atm) this will seem like a legible and clear solution:

if ( ERROR != DOSOMETHING )
{
Do something..
}
else
goto END;

Do something in between...

if ( ERROR != DOSOMETHING#2 )
{
Do something...
}
else
goto END;

END:
DeleteHandle(something);
delete Object;
return ERROR;

Now we are using goto, but the meaning is intact, and it is easy to follow in my opinion, because END: is just at the end of the function... makes sense.

The problem with gotos expands exponentially when you start using more than one. That's when it becomes hell trying to figure out where they all end up even if you have nice, meaningfull labels. As a rule I try not to use goto when possible (just to make other happy) and never ever use more than one goto in one function.

>

-------
Homepage: http://students.washington.edu/andrey

[edited by - Wreakon on December 17, 2002 5:52:46 PM]
-------Homepage: http://www.pclx.com
Arild Fines
Arild Fines
I wrote a goto the day Dijkstra died.



For those who believe in God, most of the big questions are answered. But for those of us who can''t readily accept the God formula, the big answers don''t remain stone- written. We adjust to new conditions and discoveries. We are pliable. Love need not be a command or faith a dictum. I am my own God. We are here to unlearn the teachings of the church, state, and our educational system. We are here to drink beer. We are here to kill war. We are here to laugh at the odds and live our lives so well that Death will tremble to take us -- Charles Bukowski
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Beer Hunter
Beer Hunter
quote:
Original post by Wreakon
...although I use [goto] in instances like this piece of code:

<gotos for error handling>
In c++, I''d prefer to use throw for such things. Much of the code would need to be written differently, but the result is quite readable.
SabreMan
SabreMan
quote:
Original post by DrPizza
Unless you''re using a powerloop.

A power loop is not justification for heavily nested expressions, and neither does such a pathological case make for a counterpoint. Heavily nested expressions are dreadful style.
SabreMan
SabreMan
quote:
Original post by MrPoopypants
I think goto''s are wonderful, but what do you guys think/know about them?

Your best bet is to read why Dijkstra claims Go To Considered Harmful.
jonbell
jonbell
I agree goto is bad practice but i disagree with the above poster, theres nothing wrong with occasional use of ''break''
Stan100
Stan100
I like to use goto. It''s not in all my code, or in mass quantities, but still you can''t easily have a 200 line while- statement (unless it''s a tictactoe loop)

Beatles are the best!
-----If you thought I was helpful, rate me down.If you thought I wasn't helpful, rate me down as well.This idiot didn't read my signature and tried to insult me.
johnnie2
johnnie2
Right, the goto holy war. At this point I'd like to reference an interesting story on behavior I read a while back:

quote:

Psychologists have conducted what has become a classic monkey experiment: They put a number of monkeys into a large cage, in the center of which they place a banana hung above an insulated staircase. Eventually, one of the monkeys will climb the stairs to try to get the banana. When he does, all of the other monkeys receive an electric shock through the metal floor of the cage. This is repeated for several days every time a monkey tries to climb the stairs.

Subsequently, after the shocks are no longer administered, when a monkey tries to approach the stairs, the other monkeys will attack it to keep it from climbing them. They have learned to protect themselves by keeping their fellow monkeys away from the staircase.

Next, one of the monkeys is replaced with a new one which has never been shocked. The new monkey will try to ascend the stairs, but the trained monkeys will attack it to stop it. The new monkey quickly realizes that if it approaches the stairs, it will be attacked, so it learns to stay away.

One by one, each of the original monkeys is replaced with new ones. As each new monkey approaches the stairs, he is attacked by all of the monkeys, even the ones who were never shocked. They have learned the behavior from the other monkeys. Each new monkey in his turn learns to attack any monkey who approaches the stairs, even though it has no idea why it does it.

When all the monkeys have been replaced, and even though none of the monkeys in the cage has ever been shocked, no monkey will approach the stairs. Why? If you could ask one of the monkeys, he'd probably tell you "That's the way we've always done it here."



So please, don't automatically assume that the use of goto is evil. If it works well with your implementation, use it. You're intelligent enough to determine when it's been overused.

Of course the only problem with that reasoning is that you'll continually conflict with your teacher, so you have two choices. Either just buckle down and find a way around goto (not extremely difficult since they don't come up too often even in the code of programmers who favor it), or try to convince your teacher that competent programmers can manage their own code.

RapscallionGL - arriving soon.

[edited by - johnnie2 on December 17, 2002 6:42:10 PM]
____________"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C
Anachronism
Anachronism
I refused to use 'gotos' back in my QBasic days. It seems so sloppy chasing those around. There really no use for them if you program logically.

Anonymous Poster: Nice example, how many times do you have 10 nested for loops? Before your program gets to that point it should be broken up into more modular units... Goto is the best option to escape from 5 out of the 10 for loops? Maybe... But 10 for loops isn't the best option...

I suppose in a trouble shooting situation goto's could serve a purpose... You'd catch me with my hand in Sedrick The Entertainers pants before you caught me doing that though. And despite what people will tell you, that IS long off...

And to whoever said their instructor doesn't let them use 'breaks' Your instructor is an idiot. In the real world it comes up. I can see him telling his class that just to make them have to think logically, but in the real world that's not sloppy or impractical. Same with using 'continues.' Kick your instructor in the nut cluster for me.

-Dennis

[edited by - anachronism on December 17, 2002 6:46:06 PM]

Topic Locked

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

Sign in to reply to this topic.