ma****@gmail.co m wrote:
I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?
`goto`, not `go_to`.
Roughly: using goto means that the structure of your code needs
to be unscrambled by connecting the gotos (the goestos?) to the
corresponding label. This is tedious and prone to error when the
code changes [1]. /Usually/, the syntactically obvious control
structures -- if, while, for, switch, the humble semicolon -- will
do a perfectly good job of expressing structure, and with the
addition of the slightly edgy `break`, `continue`, and `return`,
you're covered for most things you'll meet in practice.
/Sometimes/ those control structures won't do the job well
enough -- for example in a error situation when you're faced
with Standard Situation N [2] -- and then a goto or two
may be the clearest way to solve the problem. (Think of this
as hand-implementing the try-catch construct C doesn't have,
more so if you use setjmp/longjmp in place of goto). But if the
situation doesn't require it, don't use it, otherwise you'll
mislead the reader into thinking either that they've missed
something, or that /you/ have.
Peeking at my interpreter code here, I see that `goto` appears
in:
(a) generated code from flex & bison, so that doesn't count
(any more than the branches and jumps in compiled code do)
(b) in /one/ other module, where it's part of the error recovery
code in a bytecode [3] interpreter: when assorted operations
break (eg trying to add a string to an integer) then control
has to pass to a place where the interpreter can implement
"throw an exception".
Otherwise there's no gotos. (This is about 20K lines of commented
non-header code, not big but hardly trivial.) But I do use
`return` not at the bottom of a function, and `break` to get out
of loops from time to time, although that's mostly for finishing
a switch-case: if I were in a bad mood, I might count those as
gotos. I don't seem to have used `continue` at all ...
Can any one here shed some light?
Heat is, alas, more likely for this subject.
[1] Rule X: the code will have to change, /especially/ the code
for which there are promises signed in blood that it will
never change.
[2] When the only important thing is to get out, and get out NOW.
[3] For values of `byte` that are `short`.
--
Chris "spinning dizzily" Dollin
Scoring, bah. If I want scoring I'll go play /Age of Steam/.