Sujet : Re: goto considered helpful
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.cDate : 13. Dec 2024, 14:50:26
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vjhe33$3ema7$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 12.12.2024 22:50, Keith Thompson wrote:
Backward gotos tend to be dangerous. Forward gotos are less so.
I think I agree.
Dijkstras original "Go To Statement Considered Harmful" letter was
written in 1968, at a time when many languages didn't necessarily
have the structured control constructs we've come to expect since
then. Using goto to implement a loop is almost always a bad idea,
but it's something that a modern C programmer probably wouldn't
even consider doing.
Also in languages with control constructs it could be observed that
there was a tendency to write "spaghetti-code" with 'goto'. And yes,
that observation was certainly the reason for Dijkstra's paper.
I agree that gotos have valid uses.
I'm unable to provide references, but from faint memories I recall
that exiting deeply nested loops was academically mentioned to be
such a "valid" use of 'goto'. - And, frankly, I've rarely seen (or
felt the need) for [sensible] other uses of 'goto'. One other use
was to implement an (explicitly programmed, not table-driven) FSA
(but personally I'm using other methods even for that application
case).
Here's an answer I wrote on Stack Exchange some years ago to the
question "Is using goto ever worthwhile?" :
[...]
I would support adding named loops and labeled exit/continue to
a future version of C. I've used languages that have similar
features, and have found them very useful. Given that C doesn't
have multi-level break, I tend to agree that a goto statement is a
reasonable way to simulate it, often better than the alternatives.
Yes, that might be the case.
I'm not so sure about the idea of adding yet more jump-types. I
consider 'break' as a camouflage to not make that "evil goto" too
obvious and to restrict its "wildness" to a safer program context
(that also makes things like stack-unrolling etc. superfluous).
Give how rarely (IME) they seem to be used one 'goto' seems to be
enough - but 'break' and 'continue' are useful in specific types
of program logic, though 'break' with numbers as arguments (that
I used once or twice in Unix shell) I'd not consider to be a good
idea; labels would certainly be better.
[...]
Here's an example of a small C program that completely avoids the
use of goto statements. I reserve the right to ridicule anyone who
takes this program seriously.
Thanks for the warning! :-)
[ source code with lots of setjmp/longjmp ]
Janis