|
|
| How bad is a goto statement? |
|
HardTranceFan
Member #7,317
June 2006
|
Before anyone thinks about flaming me, no, I don't use goto myself. This comment got me thinking about it. Assembler (Z80) has various jump instructions, some conditional, followed by the memory location to jump to. These differed to a call instruction in that once you jumped, you couldn't return (the address jumped from wasn't pushed onto a stack, whereas the calling address was). With this in mind, is it wrong to use the occasional goto in code, other than for aesthetic reasons? -- |
|
Jonatan Hedborg
Member #4,886
July 2004
|
I have to admit that i have used in on occasion, when the alternative was to re-write a loop and add a bunch of variables and if's. Not in a large program however. It's just very ugly and makes it hard to read the code ------- |
|
Richard Phipps
Member #1,632
November 2001
|
It's not wrong to use it were it is appropriate. |
|
TeamTerradactyl
Member #7,733
September 2006
|
My ideas: I would say GOTO is a BadThing for 99% of the cases; most programmers could code well enough to never need something like this. I have seen a GOTO in a severely-nested function, though, which seemed to do alright. It was inside, I think, 8-9 while() and for() loops, and the point of the GOTO was "Okay, the value is now true/false. Continue with the rest of the current function!" Otherwise, he would have had to test on EVERY loop whether "done == true" (or whatever), and that was too expensive when you're dealing with a loop that was close to O(2^10) or something like that. So my answer is that you should avoid it when possible, but if you HAVE to use it, do it only for efficiency reasons like that above. You got C++? So if I got B-, I'm smarter than you? |
|
Matthew Leverton
Supreme Loser
January 1999
|
In high level languages the use of goto can almost always be replaced by some other, more elegant solution. The only place in C where I ever use goto is to break out of some large nested loop. With some other languages, you can use the syntax break label; to avoid using a goto in such a scenario as that. Even in C, it's quite possible that such a loop can be better written as a function call that returns in place of the goto. |
|
TeamTerradactyl
Member #7,733
September 2006
|
Matthew: I hadn't heard of "break label" before... That would be a useful tool if I ever start nesting... You got C++? So if I got B-, I'm smarter than you? |
|
Matthew Leverton
Supreme Loser
January 1999
|
Both Java and D use break label;. PHP uses break level;, where level is the number of loops of which you want to break out. |
|
ReyBrujo
Moderator
January 2001
|
Goto makes programs hard to read and analyze. The only use for goto is to break from nested cycles, yes, but most times you can prevent that by adding control variables to each of the loops. -- |
|
Rampage
Member #3,035
December 2002
|
Goto can also be used to implement exception-like logic in languages like C. I remember having seen file access routines which had lots of potential failure points. Instead of repeating error handling code for each point, the routines used goto, which made them easier to understand. -R |
|
Goalie Ca
Member #2,579
July 2002
|
A goto is only bad because people get lazy. Most of the time a goto is not needed (mostly only needed in low-level). Without the goto statement c and c++ are turing complete. The danger is someone could use a goto and avoid deep thinking and proper organization of the code (create a proper state machine and code it cleanly!). This is when it becomes hard/impossible to read and debug. ------------- |
|
TeamTerradactyl
Member #7,733
September 2006
|
My question is this: If you use a GOTO to break out of a function, what happens to all the local variables? Are they taken out of scope, or can they still be accessed? Oh, and I think I should start doing this from now on:
EDIT: Fixed the labels per Simon Parzer's feedback. Thanks, Simon! You got C++? So if I got B-, I'm smarter than you? |
|
Rampage
Member #3,035
December 2002
|
In C goto can't take you outside the scope of the current function. Quote: Oh, and I think I should start doing this from now on: Oh no! Don't ever do that! That's what the switch clause is for. -R |
|
Simon Parzer
Member #3,330
March 2003
|
Why don't you try it? You got it all wrong BTW... You do labels and gotos in C like this: ThisIsALabel:; //some code goto ThisIsALabel;
I don't know exactly what you posted but I think the syntax is wrong. |
|
HardTranceFan
Member #7,317
June 2006
|
Hey TT, your code put a grin on my face. -- |
|
orz
Member #565
August 2000
|
Sometimes, when writing my flow control, its difficult to implement it or think of it in terms of the standard C/C++ flow control primitives of for/while/do/switch loops and function calls. Usually in such cases I'll write what I'm thinking of in terms of loops and function calls, but do most of the flow control with if...break; and if...continue; statements and a few variables declared for the sole purpose of acting as flags for the loops. Occaisonally though, the code will become harder to maintain, and/or debug if I write it that way, so I'll use goto statements instead. That has only happened maybe 10-20 times in the last 30 thousand lines of C/C++ code I've written, and some of those cases could have been expressed as exceptions more naturally if I hadn't been trying to avoid some issue involved with exceptions in the relevant code. Even more rarely, sometimes I'll write something where I have such a difficult time even concieving of the flow control necessary that expressing it in terms of anything other than the natural flow control primitives I concieved of it in (often gotos for something like that) would take a great deal of diagramming and analysis and just be asking for trouble, so I write it as I concieve of it. That has only happened maybe 2 or 3 times in the last 30 thousand lines of C/C++ code I've written. Perhaps I should spend the time and effort diagramming and analyzing such cases; perhaps it would improve my ability to think in terms of the usual flow control primitives. But I'm not too sure about that. Perhaps that would merely limit my thinking to things that can easily be expressed in C/C++ instead. |
|
Rick
Member #3,572
June 2003
|
I have never used goto in C/C++, and the only time I've ever used it is with VB 6 for error handling. Because I never use it, I never think about it, and therefore when coding, my mind never even considers it. ======================================================== |
|
Audric
Member #907
January 2001
|
Ladies and gentlemen, I give you: the computed goto in C.
Compared to a switch() statement, this was way cooler IMHO, and the emulation time went from 3% to 2% (precision of this figure is low, though) |
|
Johan Halmén
Member #1,550
September 2001
|
I've never used goto. I do think it is a bad thing. The more you use goto, the more your code differs from what is considered typical C code structure. As Matthew said, you can put the part of code in a function and have returns instead. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
TeamTerradactyl
Member #7,733
September 2006
|
Johan, I would agree with that, unless there has to be a lot of data transfer. Say, for example, that you have declared a bunch of data at the beginning of your function. Then, you want to test whether "a == b" and "c < d" or "e != f", etc. If you push all that data into a separate function just to do the test, it slows down the code quite a bit. If, however, you break out of a very-nested group of loops with a GOTO, you can continue with all the data and variables you had declared/changed/etc. as if you had simply "break"-ed a bunch of times to get there... You got C++? So if I got B-, I'm smarter than you? |
|
Kitty Cat
Member #2,815
October 2002
|
Here's some code I have for decoding macroblocks from an MPEG-1 data stream:
It's pretty clear to see the code flow, IMO. 'goto block_start', and it goes to decode the beginning of a block. 'goto slice_start', and it goes to start decode the beginning of a series of blocks. 'goto next', and it goes to find the next block of the slice. Compare that with a double nested loop (adding two more indentations to a good portion of the code), extra if checks, ambiguous 'continue' and 'break' commands, and needing to trace close brackets to see where something loops back to.. PS: Whee, having PMBtab0[code] in my code breaks the syntax parser. -- |
|
Billybob
Member #3,136
January 2003
|
Quote: How bad is a goto statement? goto jail; _________________________________________________ |
|
tobing
Member #5,213
November 2004
|
We have managed to produce way over one million lines of C++ code with the only goto statements being in the various implementations of the quicksort algorithm. So I would advocate to not use goto, except when it makes algorithms easier to read, which is most often not the case. So, IF the algorithm is easier to read and understand using goto, then use it (but with great care!)... |
|
Kris Asick
Member #1,424
July 2001
|
When I was still learning how to program I would constantly use goto. Especially when I was first learning BASIC because the gosub command didn't make sense to me. Learning C/C++ eventually taught me why goto was stupid for most situations. But, I do still use it, though only in one very specific situation: To restart a procedure. My goto statements always return an executing procedure right back to where it started, or as close as is necessary for the purpose at hand, thus eliminating the need to have multiple nested do loops or recursion. Goto is generally avoidable, but in the uncommon chance that it makes your code MORE readable than a do loop or if statement, then by all means, use it. --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
|
HoHo
Member #4,534
April 2004
|
Quote: goto jail; When I was coding in QBasic I used to have label for a part of code that printed some generic error message and exited the program. It was always called hell __________ |
|
raccoon
Member #7,478
July 2006
|
I think Sun has a good reason to not even include the goto-statement in Java. |
|
|
|