R
Ronald Bruck
Joe Wright said:I don't claim goto evil except that it destroys program structure. ITom said:you can write any code that uses a goto, without a goto.
Goto is disliked, as it leads to code that is harder to read. You
should try to eliminate goto's as it will make somebody(other then you)
able to edit your code.
Common newbieism. Try code like
----
if (func1() != OK) { goto ERR; }
stmt1;
if (func2() != OK) { goto ERR; }
stmt2;
if (func3() != OK) { goto ERR; }
stmt3;
return OK;
ERR:
/* err occured, deal with it */
---
versus the "smart" way
---
if (func1() != OK) { deal_with_error; }
stmt1;
if (func2() != OK) { deal_with_error; }
stmt2;
if (func3() != OK) { deal_with_error; }
stmt3;
----
Where deal_with_error could be say 10-25 lines of cleanup code
depending on the function. Now pretend you're writing a function that
has 40 blocks like that [say doing a lot of failable bignum math
calls].
goto is no more evil than the "do" keyword. I could just as easily
write incomprehensible switch statements too...
switch (mychar == 'Y') {
case 0: do_something();
default: or_not();
}
etc, etc, etc.
Anyone who claims "goto" is evil basically hasn't written a
sufficiently complicated enough program yet.
Tom
have only ever used goto in little test programs to see how it works. I
am much more a fan of break and continue to defeat looping blocks.
I am reminded of an article I read at the height of the "GOTO"
controversy, way back in the 1970's: the author proposed
(tongue-in-cheek) a "COMEFROM" instruction. And he gave an example to
compute the factorial of 3.
The problem with the "COMEFROM" obstruction, of course, is that you had
to scan THE ENTIRE REST OF THE CODEBASE to see if you were supposed to
divert to that instruction. His sample program to compute 3! was the
worst spaghetti I ever saw.
Wikipedia has an interesting article on the concept. As they point
out, breakpoints are actually a form of comefrom.