|
|
Warp wrote:
> I really think that when Dijkstra wrote "goto considered harmful" he
> really meant "goto", as in "jump to a label", and not other control
> structures (such as returns, loops and conditionals).
I think when he wrote it, he was primarily addressing a generation of
FORTRAN programmers. :-) The people he was talking to generally didn't
have things like "if/then/else" in the language they came from.
> The idea is that "goto" (ie "jump to a label") makes the code more
> obfuscated than other control structures, and usually needlessly so.
Right. Structured programming is where each statement has an entrance
and an exit. That's what (IIRC) Dijkstra was talking about.
Returning from the middle of a function also *can* make code more
obfuscated. Certainly it isn't structured. Maintaining a function with
internal returns when you need to put something at the end is as bad as
making a function work that can throw exceptions when you have no
"finally" type of functionality.
Take a look at something like Eiffel, where there's no goto, no return,
no break or continue, and exceptions either abort your function or start
over at the beginning of it, depending on whether you catch them or not.
I can imagine situations where a goto might be handy in such a language.
"break" and "continue" and "return" and "catch" are all just limited
versions of "goto" that give you the way to do the good stuff with
"goto" without giving you the way of doing the "bad" stuff with goto.
> I don't think goto is inherently evil. I think it's inherently unnecessary,
> and overusing it is a bad idea. In the vast majority of cases there are
> better alternatives.
Agreed. But again, it depends on the language. Something like the
original version of Pascal? Not uncommonly the best way to get out of a
nested loop, or to have several bits of code all clean up in a "finally"
kind of way. Something like the original BASIC? Unquestionably
necessary as it was the only way to build a while loop.
Much of the control structure research in "normal" languages
(single-threaded etc) consists of better ways of expressing "goto". :-)
>> And then as programming languages evolve, they incorporate the good
>> places to use a goto into the syntax of the language and call it
>> something else, like "try/catch" or "return".
>
> At least those state more clearly their intention and are not so easy
> to misuse.
Exactly. And take them out of the language (like when Dijkstra was
writing) and he's talking about using "goto" in good ways, namely in
structured ways.
--
Darren New / San Diego, CA, USA (PST)
"That's pretty. Where's that?"
"It's the Age of Channelwood."
"We should go there on vacation some time."
Post a reply to this message
|
|