home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / software / 4944 < prev    next >
Encoding:
Text File  |  1992-12-14  |  5.1 KB  |  128 lines

  1. Newsgroups: comp.software-eng
  2. Path: sparky!uunet!pmafire!news.dell.com!swrinde!cs.utexas.edu!sdd.hp.com!saimiri.primate.wisc.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!hamblin.math.byu.edu!news.byu.edu!ux1!fcom.cc.utah.edu!provo.ced.utah.edu!bryant
  3. From: bryant@ced.utah.edu (Bryant Eastham,MEB 3116,581-5353)
  4. Subject: Re: C code Layout
  5. Message-ID: <1992Dec14.175533.8400@fcom.cc.utah.edu>
  6. Sender: news@fcom.cc.utah.edu
  7. Reply-To: bryant@ced.utah.edu
  8. Organization: Center for Engineering Design
  9. References: <1992Dec14.074411@eklektix.com>
  10. Date: Mon, 14 Dec 92 17:55:33 GMT
  11. Lines: 115
  12.  
  13. In article 074411@eklektix.com, rcd@raven.eklektix.com (Dick Dunn) writes:
  14.  
  15. >If you don't understand why K&R is "about right", you need to stop and
  16. >think about the real significance of it before doing any more pontificating
  17. >about commenting code.  This is not an "Is Too!"-"Is Not!" matter; it is a
  18. >theory-vs-practice matter.
  19. >
  20.  
  21. Sorry if I made it appear that way (Is Too! - Is Not!).
  22.  
  23. >Next, step back and look at where UNIX has gone.  That code has been read
  24. >and understood by tens of thousands of people, and has been successfully
  25. >modified over and over again for many years, often by people with almost no
  26. >training or aptitude.  Like it or not, there is massive evidence that the
  27. >style DOES work, IS understandable, and IS maintainable.  It's *not* the
  28. >only workable style!  But it works.
  29. >
  30.  
  31. I suppose that explains why the examples of poor (very poor) commenting
  32. style for my senior lab class were from the source for UNIX! No lie,
  33. one assignment was to comment the command "strings", which (from the
  34. distribution) had (if I remember right) ONE COMMENT LINE. I don't know
  35. who that style works for. Certainly not anyone in the class or the
  36. instructors.
  37.  
  38. >Some of this is like trying to explain Thoreau to people who like gothic
  39. >cathedrals, but I press on...
  40. >
  41.  
  42. Some of us feel the same way.
  43.  
  44. >back to Bryant, who talks about the difference between useless comments,
  45. >like:
  46. >>   i = i + 1;   /* Increment i */
  47. >(no argument here, of course)
  48. >
  49. >....and useful, necessary explanations like:
  50. >
  51. >>   /*
  52. >>    *  We are now done with the code for widget i, and while
  53. >>    *  the "for" loop will increment i again, we want to skip
  54. >>    *  the next widget and so we increment i here as well.
  55. >>    */
  56. >>
  57. >>   i = i + 1;
  58. >
  59. >The *idea* of commenting this situation is good.  You don't want somebody
  60. >looking at it, wondering whether the increment is supposed to be there or
  61. >was left over from a different sequence, etc...it's a good situation for
  62. >stopping to describe what's going on.  It's the realization that is bad.
  63. >(To a C programmer, the first problem is that the code should be
  64. >    i++;
  65. >You don't ignore the idioms of the language unless you're trying to confuse
  66. >the people reading the code.  I thought we were trying to avoid that!)
  67. >
  68.  
  69. i++ vs. i = i + 1? What was that about Gothic cathedrals??
  70.  
  71. >Next, you've got seven lines for a very simple task: one line of code, one
  72. >blank line, two gratuitous ornaments, and three lines of loquacious
  73. >description in which the real point ("skip the next widget") comes late in
  74. >the sentence.
  75. >
  76.  
  77. I agree. The example was not to be taken quite so literally, but believe
  78. me I have code where a simple increment needs that much explanation. We
  79. just finished our last project (this is my work now) at just over
  80. 75,000 lines of code. The people I work with are glad that I comment
  81. the way I do. As you said, if it works...
  82.  
  83. >Realizing that we're taking code out of context, but I think I understand
  84. >what Bryant is showing us here.  How about:
  85. >    i++;        /* done with widget i; skip the one after it */
  86. >The comment is still too wordy, but at least it doesn't take up 10% of
  87. >the current on-screen space to describe a single integer increment.
  88. >
  89.  
  90. Again, too wordy for one is not enough for another. The blank lines
  91. and "gratuitous ornaments" are very much a matter of personal style
  92. and if used consistantly are fine for me.
  93.  
  94. >In small programs, neither a dearth nor a surfeit of comments will do much
  95. >damage.  But, of course, small programs aren't the hard ones.
  96.  
  97. Remember that "strings" assignment? The original source fit on one
  98. page. Many students would argue the fact that small programs can be
  99. the HARDEST ones!
  100.  
  101. >Once a
  102. >program gets large, the very job of understanding enough of it to be able
  103. >to work on it is a problem.  Then, anything that doesn't explicitly help
  104. >definitely hurts.  Don't omit comments that could help explain what's going
  105. >on...but don't put something in just 'cause you're feeling guilty that you
  106. >haven't written a comment for ten lines.
  107. >
  108.  
  109. Well said.
  110.  
  111. >It *is* undeniably hard to develop a sense of what's obvious (needing no
  112. >explanation) and what isn't.  But it's part of learning to program well,
  113. >and until you learn that, it's as easy to err on the side of too many
  114. >comments as too few.
  115.  
  116. Again, I agree. Sorry to keep this going for so long. Matters of style
  117. are too hard to articulate with a keyboard. That must be why we argue
  118. about them so much.
  119.  
  120. >-- 
  121. >Dick Dunn    rcd@raven.eklektix.com   -or-   raven!rcd    Boulder, Colorado
  122. >    ...Straight, but not narrow.
  123.  
  124. Bryant Eastham
  125. bryant@ced.utah.edu
  126.  
  127.  
  128.