Why is using goto so bad?

Started by
151 comments, last by MrPoopypants 21 years, 3 months ago
quote:It does what? Make for a counterpoint?

Justify arbitrarily deep nesting; the powerloop construct makes the arbitrarily deep nesting managable.

In any case, gotos can improve clarity even for shallow nesting.

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 ***/
Advertisement
quote:Powerloop?

An iterative structure to allow loops to be nested to an arbitrary (runtime-specified) depth. They allow for deep iterative nesting in a managable way.

Essentially, it lets you do something approximately like:
for(int i0(0); i0 < n; ++i0)    for(int i1(0); i1 < n; ++i1)        for(int i2(0); i2 < n; ++i2)            ...                for(int in(0); in < n; ++in)                {                    doSomethingHere();                } 

where ''n'' varies at runtime.

Certain traditionally recursive problems lend themselves nicely to such an approach; N-Queens, for instance.
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 ***/
I''ve been using C++ for years now and have never even considered using a goto. Once I learned what a function was in QB I stopped using gotos even in BASIC. I didn''t even know C++ had a goto until a few years ago.

If I need to get out of a loop I set the variable of the loop equal to a condition that ends it. It''s very rare that the loop variable''s value is referenced outside the loop. If another variable needs the value it''s given it before the loop variable is modified to cause the loop to exit.

for(j=0;j<10;j++)
if(blah)
j=10;

for loops also allow you to do things like

for(j=0;j<10 && keepgoing;j++)
if(blah)
keepgoing=false;

Basically if I need an out, I give myself one. If you find yourself trying to figure out how to not use a goto then your program''s flow isn''t thought out well enough or you havn''t learned the language properly to know the far better alternatives.

I''d have to think really hard to come up with a way to use a goto that made sense.

Ben


IcarusIndie.com [ The Labyrinth | DevZone | The Wall | Hosting | Tiberian Merchandise | GameShot | Fun With Cutouts ]
Just a warning, monkeying with the loop variables can have the same consequences and can be as confusing as break statements if used excessively.

Tom L
refrain_from_stupidity( &me );
i didn''t even know C or C++ had a goto.
The usual statement that goto's shouldn't be used because it might be hard for others to follow where it leads to, or because it makes your code look messy, doesn't convince me at all. I could call a function that is like a million lines away from it's caller. Wouldn't that be hard to follow too? Does anyone keep the functions close together so that if someone wants to read the code can find them quickly? Eck, sounds stupid. Ctrl + f does the trick.

The 'rule' (if we can call it that) says that goto gives bad programming habits. You CAN use it, of course. But you should avoid it. Goto is very tricky. It seems harmless and many people are used to using it on other languages (some of them give you no choice). You may exit a loop with a goto if it'll help you. Just make sure you're using it right. Use it to go to a near place, where you can easily see that you're not messing with the process' state. You should avoid it because if you get used to it you may end up using it in other, more complex, situations, where altough it might seem harmless, in fact you're screwing the program's internals. It might be very hard to spot the problem. Compilers won't help you verify if a goto makes sense. Search google for some real-world examples. It's true.

I've used a lot of goto's on some languages, but very few on C and none in C++. I know it might get me to trouble so i supose i'm doing the right thing avoiding it. But hey, don't erase it from C or C++ 'cause you never know, I might need it tomorrow.

Blew

[edited by - Blew on December 18, 2002 2:13:37 AM]
- blew
the only possible reason i can think of to use goto is to get out of a loop, and there is break for that.
First of all, if you don''t know (or didn''t know) that c/c++ had/has goto then I would question your knowledge in the language. Secondly the use of switch is useful as long as you have sequential cases if not then the switch statement will be poorly optimized and only be benficial for coding simplicity. Moreover the switch statement has a limitation on how many cases you can have which is not the case with a if ... goto solution. Even if else has a limitation. Not that I think many actually are aware of this. A switch statement is also limited to unsigned integers only. I also do not consider Carmack to be a "god". My statement was/is more a moral support for those who listen too much to people saying you should stay away from goto or any other language function. There is no reason why you should not use any of the tools available to you, which is why Carmack is using them. Claiming that his intelligence is an excuse for using goto and therefor can get away with it is poor indeed. Not using the tools available limits you and I dare any teacher/"expert" challenge me on this point. goto can be used where other solutions either are longer and slower or problematic at best.
break only breaks on one level. If you are three levels in you are better off with a goto.
I seldom use goto but it happens. When I do it feels good to defy what I have been told by sheep.

My teacher was very much against goto and I thought it was stupid too refuse by principle to EVER use it. I agree that the use is limited but sometimes goto is nice.

Sometimes by using a goto in your function you can avoid adding another function which will contain only a few lines, be hard to give a proper name and clutter up the .h file.

[edited by - granat on December 18, 2002 5:18:43 AM]
-------------Ban KalvinB !

This topic is closed to new replies.

Advertisement