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

  1. Path: sparky!uunet!igor!thor!rmartin
  2. From: rmartin@thor.Rational.COM (Bob Martin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: GOTO, was: Tiny proposal for na
  5. Message-ID: <rmartin.715001372@thor>
  6. Date: 28 Aug 92 11:29:32 GMT
  7. References: <714668024@thor> <6800007@tisdec.tis.tandy.com> <rmartin.714863091@thor> <2318@devnull.mpd.tandem.com>
  8. Sender: news@Rational.COM
  9. Lines: 92
  10.  
  11. rgp@mpd.tandem.com (Ramon Pantin) writes:
  12.  
  13.  
  14. |It is well known that to rewrite code that uses breaks, continues,
  15. |mid-function returns and gotos (in the most general case) you have to
  16. |either:
  17. |    - Add state variables to the head of loops and additional tests
  18. |      of these variables within the bodies of the loops; or
  19. |    - State variables only tested at the head of the loops and
  20. |      code duplication.
  21.  
  22. Yes, sometimes "duplication" is necessary.  I'll discuss why this is
  23. less of a problem than it seems further on.
  24.  
  25. |So you either duplicate code (as you did in the example a couple of
  26. |postings ago) or you have to add additional tests within the loops
  27. |(which I showed in my "structured" version of the original C example).
  28.  
  29. |So your wins are:
  30. |    - Code duplication, difficult to maintain (in one of your
  31. |      previous posts you din't even seem to agree that code
  32. |      duplication was difficult to maintain!); or
  33. |    - A complexity explosion in your code (number of code paths +
  34. |      tests and sets of state variables).
  35.  
  36. The wins are, separation of all paths into structured blocks with
  37. single entry and single exit.  This is not a complexity explosion, it
  38. is an enumeration of the complexity that already exists.  The pathways
  39. that you would combine by using breaks and continues, I enumerate by
  40. using if/else and "duplicating code".
  41.  
  42. Now, dupication of code seems like a problem.  And in the strict
  43. sense, it is.  Clearly, if there is a bug in the code, it must be
  44. fixed in all of its duplicated locations. 
  45.  
  46. Or does it?  In fact, often the bug is related to the particular
  47. condition that is being executed.  When paths are combined, the same
  48. stretch of code can be executed under varying conditions.  A
  49. maintainer may find a bug in a fragment of code and "fix" it, only to
  50. find that his fix introduces another bug when that code is being
  51. executed under different conditions.
  52.  
  53. However, if the code is "duplicated" into each of the paths which
  54. represent different conditions, then the bug can be fixed insitu.
  55.  
  56. Clearly this is not a perfect solution, but I contend that a program
  57. structure which separates all paths is easier to maintain than one
  58. which combines paths using break and continue.
  59.  
  60. As for duplication, if you are forced to duplicate more than "N"
  61. lines, where "N" is relatively small; and if the duplicated segment
  62. forms a cohesive unit which performs a well defined function; then you
  63. should consider writing it as a separate function.
  64.  
  65. |From my informal counts in this thread it seems that most people are
  66. |perfectly happy with break, continue and mid-function returns.  Some
  67. |group of people avoid gotos as a rule of thumb but wouldn't mind using
  68. |them if required, some of these have not used a goto in the last 10 years.
  69. |A smaller group (only you?) avoid all of these contructs completely.
  70.  
  71. I can assure you, it is not only me.  There has been a fair amount of
  72. private mail on the subject.  However your observation is correct.  It
  73. does seem that many programmers are quite happy using break and
  74. continue.  Moreover, it seems that a smaller, but significant number
  75. of programmers are content to use goto rather than structure their code.
  76.  
  77. Perhaps this is a contributing factor to the software crisis in this
  78. country, and to why our bug rate per line of code is 100 to 1000 times
  79. greater than it is in Japan.  I have not seen a study of such a
  80. relationship, and I don't know how closely the Japanese adhere to the
  81. principles of Structured Programming, but it is thought provoking, at
  82. least to me.
  83.  
  84. |In any case Bob, nobody is suddenly going to have a religious experience
  85. |right here and convert themselves to the other camp, so if you don't see
  86. |more followups on this subject it is not because of that, but because this
  87. |subject has already been beaten to death and at least I've got tired of it.
  88.  
  89. Please don't speak for everyone.  I will accept that you are not going
  90. to have a religious experience in the near future.  But perhaps, as a
  91. result of these tiring postings, others programmers will reconsider
  92. their positions and try the same experiment that convinced me.
  93.  
  94.  
  95.  
  96.  
  97.  
  98. --
  99. Robert Martin                        Training courses offered in:
  100. R. C. M. Consulting                       Object Oriented Analysis
  101. 2080 Cranbrook Rd.                        Object Oriented Design
  102. Green Oaks, Il 60048 (708) 918-1004       C++
  103.