From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,86fd56abf3579c34 X-Google-Attributes: gid103376,public From: ncohen@watson.ibm.com (Norman H. Cohen) Subject: Re: State machines and Goto's (was Re: Should internet support software be written in Ada?) Date: 1995/04/06 Message-ID: <3m1pnf$174b@watnews1.watson.ibm.com>#1/1 X-Deja-AN: 100939416 distribution: world references: <3kaksj$iur@isnews.calpoly.edu> <3ki9t8$c8l@Starbase.NeoSoft.COM> <3ldnmh$hi5@maple.enet.net> <3line1$ma0@nic.umass.edu> <3lt00o$rgf@gnat.cs.nyu.edu> <3m0nv5$67l@theopolis.orl.mmc.com> organization: IBM T.J. Watson Research Center reply-to: ncohen@watson.ibm.com newsgroups: comp.lang.ada Date: 1995-04-06T00:00:00+00:00 List-Id: In article <3m0nv5$67l@theopolis.orl.mmc.com>, Theodore Dennison writes: |> 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? Throughout your coding standard, change all occurrences of "rule" to "guideline" and all occurrences of "shall" to "shall generally". Once these global changes are made, no further local changes are needed for your treatment of gotos. Here's an example to ponder: Consider the declarations type Node_Type; type Tree_Type is access Node_Type; type Node_Type is record Count : Natural := 0; Left, Right : Tree_Type; end record; type Bit_Vector_Type is array (Natural range <>) of Boolean; type Bit_Vector_Pointer_Type is access Bit_Vector_Type; Root : Tree_Type; BV : Bit_Vector_Pointer_Type; BV points to a bit vector describing a path (False = left, True = right) through the tree pointed to by Root. We are counting the number of times a given bit vector is encountered in the Count component of the corresponding tree node. Root starts out pointing to a tree consisting only of a root node, and new nodes are added to the tree as needed. The loop below has three exits, and the actions appropriate when one of the exits is taken is different from the actions appropriate when the other two are taken, so a goto comes in handy. Here's the code: Node := Root; Bit := BV'First; loop if Bit > BV'Last then goto Increment; end if; if BV(Bit) then if Node.Left = null then Node.Left := new Node; Node := Node.Left; exit; else Node := Node.Left; end if; else if Node.Right = null then Node.Right := new Node; Node := Node.Right; exit; else Node := Node.Right; end if; end if; Bit := Bit + 1; end loop; -- We've reached a leaf. Read the rest of BV.all while extending -- the tree along the corresponding path only. for I in Bit+1 .. BV'Last loop if BV(I) then Node.Left := new Node; Node := Node.Left; else Node.Right := new Node; Node := Node.Right; end if; end loop; <> Node.Count := Node.Count + 1; Knuth's article in the December 1974 Computing Surveys is still the best discussion I've ever seen of the goto issue. It ought to be required reading early on in any Comp. Sci. curriculum. -- Norman H. Cohen ncohen@watson.ibm.com