home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12934 < prev    next >
Encoding:
Internet Message Format  |  1992-08-26  |  2.4 KB

  1. Path: sparky!uunet!igor!thor!rmartin
  2. From: rmartin@thor.Rational.COM (Bob Martin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Tiny proposal for named loops.
  5. Message-ID: <rmartin.714863856@thor>
  6. Date: 26 Aug 92 21:17:36 GMT
  7. 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>
  8. Sender: news@Rational.COM
  9. Lines: 53
  10.  
  11. fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
  12.  
  13. |rmartin@thor.Rational.COM (Bob Martin) writes:
  14.  
  15. |>clearly "while (1)" does not fall under your advice to "code what you
  16. |>mean".  However, by using a flag, you can put "what you mean" in the
  17. |>loop condition.
  18.  
  19. |I disagree. If find while(1), or more preferably for(;;), to indicate
  20. |quite clearly what the programmer means.
  21.  
  22. Taken literally, it means: "infinite loop", i.e. a loop with no exit
  23. condition.  Clearly, this is not what you mean.
  24.  
  25. |If the termination condition for a loop is complex, then
  26. |it should be coded as simply as possible - this means using
  27. |goto/break/etc - and documented with comments.
  28.  
  29. Again, I disagree.  I find this construct:
  30.  
  31.    while (error == NO && done == NO)
  32.  
  33. easier to understand than:
  34.  
  35.    for (;;)
  36.  
  37. I disagree that breaks are the simplest possible.  They are more
  38. complex becuase they abort rather than terminate the loop.
  39.  
  40. |While we're on the subject of FSM's and gotos, have a look at this code
  41. |taken from my Turing machine simulator program. This is another good
  42. |example of where gotos are useful. Try rewriting this without gotos!
  43. |(Solutions using exceptions don't count - at least, not until my compiler
  44. |implements them!)
  45.  
  46. [...parsing code deleted...]
  47.  
  48. The code was a simple parser which discovered errors in a deeply
  49. nested loop and jumped out of the loop.
  50.  
  51. Most parsers are finite state machines.  This could have been modeled
  52. as a finite state machine, complete with a state variable and
  53. transition codes.  There are many ways to implement such FSMs without
  54. resorting to gotos.  Such methods are quite resilient to change, since
  55. they directly model the state-transition map.  But indiscrimiate use
  56. of gotos to simulate a FSM can make the FSM difficult to change.
  57.  
  58.  
  59. --
  60. Robert Martin                        Training courses offered in:
  61. R. C. M. Consulting                       Object Oriented Analysis
  62. 2080 Cranbrook Rd.                        Object Oriented Design
  63. Green Oaks, Il 60048 (708) 918-1004       C++
  64.