Usage of goto's

Henry Spencer henry at zoo.toronto.edu
Sun Sep 30 10:07:11 AEST 1990


In article <1990Sep28.121230.17767 at NCoast.ORG> ramsey at NCoast.ORG (Cedric Ramsey) writes:
>... what better way 
>do we have to implement finite state machines. A switch statement ?
>All a switch statement is a goto statement, internally that is...

All control structures are gotos "internally".  The question is, should
one use a goto or a more disciplined structure to achieve one's goals?
Usually readability is a primary goal, both so you can understand the
code yourself and so your successors can too.  More disciplined structures
win big here.  Efficiency, the little tin god of incompetent programmers,
is seldom crucial enough to be a decisive argument in favor of gotos,
although there *are* exceptions to this.

Finite-state machines are a difficult case.  The standard advantage of
disciplined control structures is that you can tell how you got to a
particular point, and generally grasp the overall structure more easily...
but in a complex finite-state machine, by definition control hops around
almost at random.  It is better to avoid doing things as complex FSMs
if you can.  If you can't, I would still say that switch gets the nod
unless absolute maximum speed is vital.  At least the switch enforces
some small modicum of organization on what is basically a disorganized
control structure (the FSM).

>Is it okay for a programmer to break a TABOO intentionally; by design?

As I've said in other threads, one should treat such taboos as wise
guidelines rather than holy law.  You should have a *good* reason for
violating them, and it takes experienced judgement to decide how good
a reason is.  If you are new to the business, you are best advised to
stick to the rules until you understand things better.
-- 
Imagine life with OS/360 the standard  | Henry Spencer at U of Toronto Zoology
operating system.  Now think about X.  |  henry at zoo.toronto.edu   utzoo!henry



More information about the Comp.lang.c mailing list