Hi, Today while I wwas coding I noted that I almost always use goto in this constructs: while(condition) { switch(some_var) { case 'y': do_something(); break; case 'x': goto end; break; } } end: Is this something not very advisable to do? Well at least that is what an author suggested.. in my short experience I can firmly say that it doesnt look so clean on my code..
On Tue, 2003-04-15 at 04:15, Raúl Gutiérrez Segalés wrote:
Hi,
Today while I wwas coding I noted that I almost always use goto in this constructs:
while(condition) { switch(some_var) { case 'y': do_something(); break;
A break is a goto too, in a sense.
case 'x': goto end;
I don't think a goto used in this way is so terrible. It's still fairly linear code. The big problem with "goto" is that if it's misused it can lead to code that is extremely difficult to follow. For example goto:s jumping out of or in to procedures. In this particular case I don't see "goto end" any more difficult to follow than "break".
break;
but if this is ever reached you should file a bug report with the compiler people :)
} }
end:
Is this something not very advisable to do? Well at least that is what an author suggested..
Some people think "goto" should be completely banned from programming (would this include JMP in assembly I wonder :) but I don't think so. It should be used with extreme caution though. Take it from someone who has had to work with maintenance of very old, very crufty and very huge source files. If used properly and carefully, it can lead to code that is even easier to read than the alternative. But if used improperly you will want to kill yourself rather than have to read the code 15 years later.
while(condition) { switch(some_var) { case 'y': do_something(); break;
case 'x': goto end; break; } }
end:
Is this something not very advisable to do? Well at least that is what an author suggested.. in my short experience I can firmly say that it doesnt look so clean on my code..
It's horrible. You mean this: looping = 1; while( looping && condition) { switch(some_var) { case 'y': do_something(); break; case 'x': looping = 0; break; } } 'looping' should obviously be replaced with a variable name describing the reason you need to continue round the loop. -- "...our desktop is falling behind stability-wise and feature wise to KDE ...when I went to Mexico in December to the facility where we launched gnome, they had all switched to KDE3." - Miguel de Icaza, March 2003
while(condition) { switch(some_var) { case 'y': do_something(); break;
case 'x': goto end; break; } }
end:
Is this something not very advisable to do? Well at least that is what an author suggested.. in my short experience I can firmly say that it doesnt look so clean on my code..
It's horrible. You mean this:
looping = 1; while( looping && condition) { switch(some_var) { case 'y': do_something(); break;
case 'x': looping = 0; break; } }
Ok, very nice BUT... if I have something done after the switch that I want to skip... while(condition) { char z; int first=0; z=getchar(); getchar(); switch(z) { case 'a': first=1; break; case 'z': goto end; /* cause I dont want it tu print the message */ break; } if(first) /* I dont want spend CPU cycles with this, just to jump over it.. */ puts("You typed the first letter of the alphabet"); } I know this is pretty ugly code and could be fixed in other ways, but I can assure the there are situations that look like this.. so what would you suggest for replacing goto here, if you have a very strong need to skip some part of you loop..
'looping' should obviously be replaced with a variable name describing the reason you need to continue round the loop.
-- "...our desktop is falling behind stability-wise and feature wise to KDE ...when I went to Mexico in December to the facility where we launched gnome, they had all switched to KDE3." - Miguel de Icaza, March 2003
-- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists/archive/suse-programming-e
Ok, very nice BUT... if I have something done after the switch that I want to skip...
while(condition) { char z; int first=0;
z=getchar(); getchar();
switch(z) { case 'a': first=1; break; case 'z': goto end; /* cause I dont want it tu print the message */ break; } if(first) /* I dont want spend CPU cycles with this, just to jump over it.. */ puts("You typed the first letter of the alphabet"); }
I know this is pretty ugly code and could be fixed in other ways, but I can assure the there are situations that look like this.. so what would you suggest for replacing goto here, if you have a very strong need to skip some part of you loop..
Well, you'd put the "puts("You typed... line inside the case block! I think what you've written above is contrived. You're saying: check condition 1 do something1 else check condition 2 do something2 else check conditon 3 do something3 check condition 1 do something else Just put "something else" in with "something1". I can't remember when I've ever seen a piece of code like that which you have above. If I did come across one, I'd structure it like you have above, with an extra condition. You're talking about a tiny number of clock cycles to check the extra condition, versus the cost in readability of jumping over a block of code. Jumping over a line or two, as in the little example, is OK, but when the code is expanded you might end up jumping over a large block. That's the problem with using gotos: they tend to spread out and multiply as the code develops. Don't get me wrong, I'm not religious about the issue. I've used gotos before, where I consider them necessary. But they tend to produce slower code, they make code harder to format because they break logical structure, and their use promotes violation of structured programming principles. I don't consider them evil, but I don't use them unless there's a really good reason. -- "...our desktop is falling behind stability-wise and feature wise to KDE ...when I went to Mexico in December to the facility where we launched gnome, they had all switched to KDE3." - Miguel de Icaza, March 2003
Am Dienstag, 15. April 2003 05:26 schrieb Raúl Gutiérrez Segalés: [...]
It's horrible. You mean this:
It is very horrible. [...]
Ok, very nice BUT... if I have something done after the switch that I want to skip...
while(condition) { char z; int first=0;
z=getchar(); getchar();
switch(z) { case 'a': first=1; break; case 'z': goto end; /* cause I dont want it tu print the message */ break; } if(first) /* I dont want spend CPU cycles with this, just to jump over it.. */ puts("You typed the first letter of the alphabet"); }
I know this is pretty ugly code and could be fixed in other ways, but I can assure the there are situations that look like this.. so what would you suggest for replacing goto here, if you have a very strong need to skip some part of you loop..
looping = 1; while (condition) { switch (any_var) { case 1: break; case 2: break; case xy: looping = 0; break; default: looping = 0; break; } if (looping == 0) { do_something(); #ifdef SKIP break; #endif } In this piece of code you set the variable looping to 0 when you want something else to be proceeded. The If-clause has to be right under the switch block. When looping is 0 it does the code you want to do and stays in the enclosing while loop. If you want to leave the while loop you can do that with the break;. You just have to define SKIP somewhere at the top of your file (or before it is used the first time). Greets Georg
On Mon, 14 Apr 2003 22:15:45 -0400 (PYT)
Raúl Gutiérrez Segalés
Today while I wwas coding I noted that I almost always use goto in this constructs:
while(condition) { switch(some_var) { case 'y': do_something(); break;
case 'x': goto end; break; } }
end:
Is this something not very advisable to do? Well at least that is what an author suggested.. in my short experience I can firmly say that it doesnt look so clean on my code.. I've read all the other responses. In my 20 years of using the C language, I have never had the occasion to use goto in my own code. HOWEVER, there are cases when you might be very deep in a nesting situation where it may be appropriate. Derek's example, while(looping && condition)
works well, but adds another test in the loop. You might have something
like:
case 'x':
/* falsify the condition */
continue;
In a recent porting project of complex code involving multi-level
locking there was a case where a goto was used such that all code would
exit through a common unlocking point.
My criteria has always been to keep code an simple and straighforward as
possible. If an occasional goto is needed, then by all means use it.
--
Jerry Feldman
On Dienstag, 15. April 2003 14:34, Jerry Feldman wrote:
I've read all the other responses. In my 20 years of using the C language, I have never had the occasion to use goto in my own code.
Same here (except that I've been programming C for just about 15 years ;-) ).
HOWEVER, there are cases when you might be very deep in a nesting situation where it may be appropriate.
Well, kind of...
Derek's example, while(looping && condition)
works well, but adds another test in the loop. You might have something like: case 'x': /* falsify the condition */ continue;
In a recent porting project of complex code involving multi-level locking there was a case where a goto was used such that all code would exit through a common unlocking point.
My criteria has always been to keep code an simple and straighforward as possible. If an occasional goto is needed, then by all means use it.
Right. Not using "goto" at all should not be taken religiously - it's a good
guide line, no more.
BUT it can in most cases easily be avoided by a premature "return" or - like
pointed out above - "continue", both of which I personally prefer in that
given situation. I'd try to avoid "break" except inside a "switch" statement,
though, because even if the code's author knows for sure how far outside this
"break" will take him, the next person to maintain that code may not - which
makes this code hard to maintain.
Anyway, all of those possibilities of course merely hide the fact that the
resulting machine code will most certainly be some kind of "jump" or "branch"
statement - which is the machine code equivalent of "goto". ;-)
Bottom line: Use your common sense. Don't be religious.
CU
--
Stefan Hundhammer
participants (6)
-
Anders Johansson
-
Derek Fountain
-
Georg Wagner
-
Jerry Feldman
-
Raúl Gutiérrez Segalés
-
Stefan Hundhammer