comp.lang.ada
 help / color / mirror / Atom feed
From: stt@spock.camb.inmet.com (Tucker Taft)
Subject: Re: State machines and Goto's (was Re: Should internet support software be written in Ada?)
Date: 1995/04/07
Date: 1995-04-07T00:00:00+00:00	[thread overview]
Message-ID: <D6o4t2.5uM@inmet.camb.inmet.com> (raw)
In-Reply-To: 3m0nv5$67l@theopolis.orl.mmc.com

Theodore Dennison (dennison@escmail.orl.mmc.com) wrote:

: dewar@cs.nyu.edu (Robert Dewar) wrote:
: >Here is the basic issue with finite state machines. A normal picture
: >of a finite state machine is a spaghetti diagram with circles for
: >states and arrows for transitions.

One reason to prefer case statements over gotos for implementing
a finite state machine is that there is no danger of forgetting
the "goto" at the "end" of a state's code.  Using gotos instead
of a "case" statement opens up the programmer to the same kind
of bug as the notorious "missing break" in C/C++ switch statements.
You might just flow through to the state that happened to follow
in the text of the program, without intending to.  

E.g.:
        goto Initial_State;

    <<state_1>>
           blah; blah;
           -- oops, I forgot the goto.
    <<state_2>>
           ...

Versus:

     State : State_Enum := Initial_State;
     Next_State : State_Enum;
 begin
     loop
         Next_State := Uninitialized_State;

         case State is
             when State_1 =>
                  blah; blah;
		  -- oops, I forgot to set Next_State
             when State_2 =>
	        ...
	 end case;

	 if Next_State = Uninitialized_State then
	     Put_Line("Next_State not set");
	     Put_Line("Previous state is " & State_Enum'Image(State));
	     raise Program_Error;
	 end if;

         State := Next_State;  -- Advance to next state
     end loop;


Of course, you could also see the goto-based approach's capability 
of "flowing through" as a nice opportunity for optimization 
(that is presumably why "break;" is not the default in 
C/C++ switch statements).  However, I believe few would 
argue at this point with 20/20 hindsight (including the most 
die-hard C programmer), that the C/C++ default of "fall through" 
between switch alternatives was the right choice.

: Which is why people today tend to design using structure charts.

: >Please be clear, I am not saying that anyone who completely avoids 
: >gotos is incompetenmt, not at all. I am saying that it is wrong to think
: >that it is appropriate to visit this absolute rule on competent
: >programmers.

: I have now worked under 3 different Ada coding standards. Every one
: of them had a "no goto's" rule. Since I'm currently editing our next
: coding standard, perhaps someone could suggest a replacement rule? It
: should be very specific about what allowable and unallowable goto's
: are. We need something that our software quality folks can enforce.
: The rule can make NO assumptions about the competency of the programmer,
: as we won't find out the truth on that matter until it is too late.

Here is a half-serious ;-} suggestion:

How about requiring the programmer to write a short paragraph
in the comments, on why the goto was the most appropriate solution
for the given problem?  Require that the paragraph be no less than
20 words, and no more than 100 words long.  The net effect would
be to create a sufficient incentive that the programmer would have
to think hard before using a goto.

: T.E.D. (structured programming mafioso)

-Tucker Taft  stt@inmet.com
Intermetrics, Inc.




  parent reply	other threads:[~1995-04-07  0:00 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-03-17  0:24 Should internet support software be written in Ada? Bill Brooks
1995-03-17  8:47 ` Anthony Shipman
1995-03-19 22:06 ` David Weller
1995-03-23 15:05   ` Theodore Dennison
1995-03-24 10:26     ` Fred J. McCall
1995-03-27  9:50       ` Robb Nebbe
1995-03-27 19:43         ` State machines and Goto's (was Re: Should internet support software be written in Ada?) Robert I. Eachus
1995-03-27 23:14           ` Arthur Evans Jr
1995-03-29  0:00           ` Dan
1995-04-01  0:00             ` Mike White
1995-04-04  0:00               ` Robert Dewar
1995-04-06  0:00                 ` Theodore Dennison
1995-04-06  0:00                   ` Norman H. Cohen
1995-04-07  0:00                   ` Robert Dewar
1995-04-07  0:00                   ` Tucker Taft [this message]
1995-04-07  0:00                 ` Mike White
     [not found]                   ` <3ma7rt$smt@kaiwan009.kaiwan.com>
     [not found]                     ` <dewar.797514490@gnat>
     [not found]                       ` <3meunj$66u@felix.seas.gwu.edu>
1995-04-20  0:00                         ` CS teachers who can't code their way outta a paper bag Richard A. O'Keefe
1995-03-27 14:24       ` Should internet support software be written in Ada? Theodore Dennison
1995-03-28  0:00         ` Robert Dewar
1995-03-28  9:32         ` Fred J. McCall
1995-03-29  0:00           ` Theodore Dennison
1995-03-29  0:00   ` Robert I. Eachus
1995-03-31  0:00     ` Theodore Dennison
1995-04-05  0:00   ` Wes Groleau
1995-04-07  0:00   ` State machines and Goto's (was Re: Should internet support software be written in Ada?) Wes Groleau
1995-04-07  0:00     ` Robert Firth
     [not found]       ` <D6qyv0.6Jv@nntpa.cb.att.com>
1995-04-19  0:00         ` Fergus Henderson
1995-04-19  0:00           ` Fred J. McCall
1995-04-19  0:00             ` George Haddad
1995-04-20  0:00             ` Mark A Biggar
1995-04-20  0:00             ` Bill Winn
1995-04-20  0:00               ` Robert Dewar
1995-04-20  0:00             ` State machines and Goto's (was Re: Sho Brian Hanson
1995-03-22 23:08 ` Should internet support software be written in Ada? Keith Thompson
     [not found] ` <3me1qs$n4a@theopolis.orl.mmc.com>
     [not found]   ` <3mevmu$8an@felix.seas.gwu.edu>
     [not found]     ` <3mh75i$8eu@rational.rational.com>
     [not found]       ` <3mjihr$iqq@felix.seas.gwu.edu>
     [not found]         ` <3mp20f$80t@kaiwan009.kaiwan.com>
1995-04-20  0:00           ` Academic CS Losers? Phillip Fanous
1995-04-21  0:00             ` Bill Winn
     [not found] ` <dewar.798207931@gnat>
     [not found]   ` <cppD78ywq.B31@netcom.com>
     [not found]     ` <3n1fsv$lgd@butch.lmsc.lockheed.com>
1995-04-20  0:00       ` Problems with proofs Robin Rowe
1995-04-20  0:00         ` Garlington KE
     [not found]     ` <dewar.798240702@gnat>
1995-04-19  0:00       ` Brian Harvey
1995-04-19  0:00         ` Robert Dewar
1995-04-20  0:00       ` Robin Rowe
1995-04-20  0:00       ` Robin Rowe
1995-04-20  0:00         ` Robert Dewar
1995-04-20  0:00         ` Robert Dewar
1995-04-21  0:00           ` Robin Rowe
1995-04-21  0:00             ` Robert Dewar
1995-04-20  0:00         ` Steve Tate
1995-04-20  0:00           ` Apology to Steve Robin Rowe
1995-04-21  0:00         ` Problems with proofs Larry Kahn
     [not found] ` <dewar.798093453@gnat>
1995-04-20  0:00   ` What good is halting prob? Max Hailperin
     [not found] ` <3mrv7h$3mq@larry.rice.edu>
     [not found]   ` <3msnu4$6am@kaiwan009.kaiwan.com>
     [not found]     ` <KUBEK.95Apr18213646@insage.gerii.insa-tlse.fr>
     [not found]       ` <cppD792GC.1uI@netcom.com>
1995-04-20  0:00         ` Teaching OO/C++ Philip Machanick
     [not found] ` <cppD75t6F.47M@netcom.com>
     [not found]   ` <EACHUS.95Apr17172831@spectre.mitre.org>
     [not found]     ` <cppD77Ex6.E77@netcom.com>
     [not found]       ` <3mve9b$gaj@news.cais.com>
1995-04-18  0:00         ` What good is halting prob? Jay M Martin
1995-04-19  0:00           ` Steve Tate
1995-04-20  0:00             ` Jay M Martin
1995-04-21  0:00               ` Steve Tate
1995-04-21  0:00             ` Ray Toal
     [not found]         ` <cppD7880n.32B@netcom.com>
1995-04-19  0:00           ` Randolph Crawford
1995-04-19  0:00             ` Robert Dewar
1995-04-20  0:00             ` Robin Rowe
1995-04-20  0:00               ` Robert Dewar
1995-04-20  0:00                 ` Robin Rowe
1995-04-21  0:00                   ` Brian Hanson
1995-04-21  0:00           ` sxc
     [not found]       ` <dewar.798172270@gnat>
     [not found]         ` <cppD786FM.1u9@netcom.com>
1995-04-19  0:00           ` Mark A Biggar
1995-04-19  0:00             ` Richard Ladd Kirkham
1995-04-19  0:00               ` Robert Dewar
1995-04-20  0:00                 ` Richard Ladd Kirkham
1995-04-20  0:00                   ` Robert Dewar
1995-04-21  0:00               ` Mark A Biggar
     [not found]           ` <dewar.798207364@gnat>
     [not found]             ` <3n0ka7$b57@hermes.unt.edu>
     [not found]               ` <dewar.798232482@gnat>
1995-04-19  0:00                 ` Mark A Biggar
1995-04-19  0:00                   ` Steve Tate
1995-04-19  0:00                   ` Robert Dewar
     [not found]             ` <cppD78xMv.49w@netcom.com>
1995-04-19  0:00               ` Robb Nebbe
1995-04-21  0:00       ` Robert I. Eachus
1995-04-21  0:00         ` Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox