home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!igor!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Newsgroups: comp.lang.c++
- Subject: Re: Tiny proposal for named loops.
- Message-ID: <rmartin.714863856@thor>
- Date: 26 Aug 92 21:17:36 GMT
- References: <aldavi01.714376080@starbase.spd.louisville.edu> <rmartin.714435942@dirac> <CHUCK.PHILLIPS.92Aug24165034@halley.FtCollinsCO.NCR.COM> <rmartin.714750452@thor> <9223902.3230@mulga.cs.mu.OZ.AU>
- Sender: news@Rational.COM
- Lines: 53
-
- fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
-
- |rmartin@thor.Rational.COM (Bob Martin) writes:
-
- |>clearly "while (1)" does not fall under your advice to "code what you
- |>mean". However, by using a flag, you can put "what you mean" in the
- |>loop condition.
-
- |I disagree. If find while(1), or more preferably for(;;), to indicate
- |quite clearly what the programmer means.
-
- Taken literally, it means: "infinite loop", i.e. a loop with no exit
- condition. Clearly, this is not what you mean.
-
- |If the termination condition for a loop is complex, then
- |it should be coded as simply as possible - this means using
- |goto/break/etc - and documented with comments.
-
- Again, I disagree. I find this construct:
-
- while (error == NO && done == NO)
-
- easier to understand than:
-
- for (;;)
-
- I disagree that breaks are the simplest possible. They are more
- complex becuase they abort rather than terminate the loop.
-
- |While we're on the subject of FSM's and gotos, have a look at this code
- |taken from my Turing machine simulator program. This is another good
- |example of where gotos are useful. Try rewriting this without gotos!
- |(Solutions using exceptions don't count - at least, not until my compiler
- |implements them!)
-
- [...parsing code deleted...]
-
- The code was a simple parser which discovered errors in a deeply
- nested loop and jumped out of the loop.
-
- Most parsers are finite state machines. This could have been modeled
- as a finite state machine, complete with a state variable and
- transition codes. There are many ways to implement such FSMs without
- resorting to gotos. Such methods are quite resilient to change, since
- they directly model the state-transition map. But indiscrimiate use
- of gotos to simulate a FSM can make the FSM difficult to change.
-
-
- --
- Robert Martin Training courses offered in:
- R. C. M. Consulting Object Oriented Analysis
- 2080 Cranbrook Rd. Object Oriented Design
- Green Oaks, Il 60048 (708) 918-1004 C++
-