[comp.lang.c] The D Programming Language: got

mcdonald@uxe.cso.uiuc.edu (03/09/88)

>There are only three reasons for using a goto.  Firstly, to implement a
>control structure not directly available in the language in question.  This
>"deficiency" is not necessarily bad; I've used goto to emulate coroutines, but
>I probably wouldn't want to see coroutines added to the language.  If you
>remove goto but then add every other possible flow construct just to avoid the
>desire for a goto, you end up with BLISS.  Let's not do that.

>Secondly, error handling, which could be considered a special case of the
>first reason in that it emulates an exception construct of some type.  This
>includes not only cleanup-and-return but also retry-operation; sometimes a
>simple backward goto is clearer than a while.

>Thirdly, you can use a goto if you really, really want to, a whole lot.  :-)
>(I use this as my universal escape clause.)

I've only used goto's for non-error handling cases once in C. This is,
I guess, an example of Heuer's third case. The particular event was
the input parser for my text adventure "World". A logic diagram for this
is, like the English language it must decipher, a bowl of spaghetti.
I tried for quite a while it untangle it, and finally gave up. The
only way I was ever able to get rid of the goto's was to define a 
"state variable" which would be set to some "magic" number at the
end of every block of code ending in a goto. Then I put everything
in a big case statement, going to the appropriate place depending on this
magic variable. The case statement was inside a do ... while(statevar != QUIT).
This gets rid of the goto's, but you are left with the magic numbers.
Do you leave them as numbers? No, this is not easy to read, so you give
them symbolic names that are #defined somewhere in the upper reaches of the
program. At this point I get thinking "Why not just leave in the goto's,
and give the labels the appropriate names?"
    It seems to me that if you reach the point where you have to use
the formally guaranteed-to-work method of removing goto's, you might as
well leave them in.