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

  1. Newsgroups: comp.software-eng
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!asuvax!ncar!csn!raven!rcd
  3. From: rcd@raven.eklektix.com (Dick Dunn)
  4. Subject: Re: C code Layout
  5. Message-ID: <1992Dec14.074411@eklektix.com>
  6. Summary: theory and practice
  7. Organization: eklektix - Boulder, Colorado
  8. References: <1992Dec12.122453.8582@seq.uncwil.edu> <1992Dec12.200619.29602@fcom.cc.utah.edu>
  9. Date: Mon, 14 Dec 1992 07:44:11 GMT
  10. Lines: 95
  11.  
  12. bryant@ced.utah.edu writes:
  13. >herbst@seq.uncwil.edu (R.T. Herbst) writes:
  14. >>[stuff about large successful projects, emphasizing concise to-the-point
  15. comments]
  16.  
  17. >Anyone can get lucky a couple of times. ;-)
  18.  
  19. Well, ;-) or no, that's a bad attitude.  Reality is trump; get used to it.
  20. It's hard to defeat something that works in practice with an argument only
  21. demonstrated on paper (or phosphor:-).
  22.  
  23. >If you believe that K&R is "about right", then we will never
  24. >come to agreement on what the right amount of comments is.
  25.  
  26. If you don't understand why K&R is "about right", you need to stop and
  27. think about the real significance of it before doing any more pontificating
  28. about commenting code.  This is not an "Is Too!"-"Is Not!" matter; it is a
  29. theory-vs-practice matter.
  30.  
  31. The style of code and the level of comment in K&R is influenced by the fact
  32. that it's a language text, as Charlie Martin pointed out.  Realize that the
  33. code examples in K&R are written to illustrate points, and are accompanied
  34. by a lot of text to explain them to people who are just learning C.  Still,
  35. keeping that in mind, the style of code and the amount/style of comments
  36. do not differ significantly from what you'd have found in the UNIX source
  37. 10-15 years ago.  In other words, Ritchie and Kernighan...and Thompson and
  38. others...wrote and write C in a style not too different from what you see
  39. in K&R.
  40.  
  41. Next, step back and look at where UNIX has gone.  That code has been read
  42. and understood by tens of thousands of people, and has been successfully
  43. modified over and over again for many years, often by people with almost no
  44. training or aptitude.  Like it or not, there is massive evidence that the
  45. style DOES work, IS understandable, and IS maintainable.  It's *not* the
  46. only workable style!  But it works.
  47.  
  48. If you want to understand the value and effect of comments, and of various
  49. commenting styles, I submit that you had better start with the understand-
  50. ing that K&R style DOES work.  If you've got the idea stuck in your head
  51. that it can't possibly be any good, then you'd better start by trying to
  52. figure out why it works.  As I said, reality is trump.
  53.  
  54. Some of this is like trying to explain Thoreau to people who like gothic
  55. cathedrals, but I press on...
  56.  
  57. back to Bryant, who talks about the difference between useless comments,
  58. like:
  59. >   i = i + 1;   /* Increment i */
  60. (no argument here, of course)
  61.  
  62. ...and useful, necessary explanations like:
  63.  
  64. >   /*
  65. >    *  We are now done with the code for widget i, and while
  66. >    *  the "for" loop will increment i again, we want to skip
  67. >    *  the next widget and so we increment i here as well.
  68. >    */
  69. >
  70. >   i = i + 1;
  71.  
  72. The *idea* of commenting this situation is good.  You don't want somebody
  73. looking at it, wondering whether the increment is supposed to be there or
  74. was left over from a different sequence, etc...it's a good situation for
  75. stopping to describe what's going on.  It's the realization that is bad.
  76. (To a C programmer, the first problem is that the code should be
  77.     i++;
  78. You don't ignore the idioms of the language unless you're trying to confuse
  79. the people reading the code.  I thought we were trying to avoid that!)
  80.  
  81. Next, you've got seven lines for a very simple task: one line of code, one
  82. blank line, two gratuitous ornaments, and three lines of loquacious
  83. description in which the real point ("skip the next widget") comes late in
  84. the sentence.
  85.  
  86. Realizing that we're taking code out of context, but I think I understand
  87. what Bryant is showing us here.  How about:
  88.     i++;        /* done with widget i; skip the one after it */
  89. The comment is still too wordy, but at least it doesn't take up 10% of
  90. the current on-screen space to describe a single integer increment.
  91.  
  92. In small programs, neither a dearth nor a surfeit of comments will do much
  93. damage.  But, of course, small programs aren't the hard ones.  Once a
  94. program gets large, the very job of understanding enough of it to be able
  95. to work on it is a problem.  Then, anything that doesn't explicitly help
  96. definitely hurts.  Don't omit comments that could help explain what's going
  97. on...but don't put something in just 'cause you're feeling guilty that you
  98. haven't written a comment for ten lines.
  99.  
  100. It *is* undeniably hard to develop a sense of what's obvious (needing no
  101. explanation) and what isn't.  But it's part of learning to program well,
  102. and until you learn that, it's as easy to err on the side of too many
  103. comments as too few.
  104. -- 
  105. Dick Dunn    rcd@raven.eklektix.com   -or-   raven!rcd    Boulder, Colorado
  106.     ...Straight, but not narrow.
  107.