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

  1. Newsgroups: comp.software-eng
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!spool.mu.edu!yale.edu!ira.uka.de!slsvaat!josef!kanze
  3. From: kanze@us-es.sel.de (James Kanze)
  4. Subject: Re: C code Layout
  5. In-Reply-To: rcd@raven.eklektix.com's message of Mon, 14 Dec 1992 07:44:11 GMT
  6. Message-ID: <KANZE.92Dec15181119@slsvdnt.us-es.sel.de>
  7. Sender: news@us-es.sel.de
  8. Organization: SEL
  9. References: <1992Dec12.122453.8582@seq.uncwil.edu>
  10.     <1992Dec12.200619.29602@fcom.cc.utah.edu>
  11.     <1992Dec14.074411@eklektix.com>
  12. Date: 15 Dec 92 18:11:19
  13. Lines: 84
  14.  
  15. In article <1992Dec14.074411@eklektix.com> rcd@raven.eklektix.com
  16. (Dick Dunn) writes:
  17.  
  18. |> If you don't understand why K&R is "about right", you need to stop and
  19. |> think about the real significance of it before doing any more pontificating
  20. |> about commenting code.  This is not an "Is Too!"-"Is Not!" matter; it is a
  21. |> theory-vs-practice matter.
  22.  
  23. |> The style of code and the level of comment in K&R is influenced by the fact
  24. |> that it's a language text, as Charlie Martin pointed out.  Realize that the
  25. |> code examples in K&R are written to illustrate points, and are accompanied
  26. |> by a lot of text to explain them to people who are just learning C.  Still,
  27. |> keeping that in mind, the style of code and the amount/style of comments
  28. |> do not differ significantly from what you'd have found in the UNIX source
  29. |> 10-15 years ago.  In other words, Ritchie and Kernighan...and Thompson and
  30. |> others...wrote and write C in a style not too different from what you see
  31. |> in K&R.
  32.  
  33. If my memory serves me right, Kernighan and Ritchie actually say in
  34. their book that the commenting there is lighter than it should be in a
  35. production program, so that the programs don't take up too much space
  36. on the page.  This is a valid argument for example programs in books,
  37. where the text already says more than you would ever put in comments,
  38. but Kernighan and Ritchie themselves apparently didn't think that this
  39. style was adequate for production code.
  40.  
  41. |> ...and useful, necessary explanations like:
  42.  
  43. |> >   /*
  44. |> >    *  We are now done with the code for widget i, and while
  45. |> >    *  the "for" loop will increment i again, we want to skip
  46. |> >    *  the next widget and so we increment i here as well.
  47. |> >    */
  48. |> >
  49. |> >   i = i + 1;
  50.  
  51. |> The *idea* of commenting this situation is good.  You don't want somebody
  52. |> looking at it, wondering whether the increment is supposed to be there or
  53. |> was left over from a different sequence, etc...it's a good situation for
  54. |> stopping to describe what's going on.  It's the realization that is bad.
  55. |> (To a C programmer, the first problem is that the code should be
  56. |>     i++;
  57. |> You don't ignore the idioms of the language unless you're trying to confuse
  58. |> the people reading the code.  I thought we were trying to avoid that!)
  59.  
  60. |> Next, you've got seven lines for a very simple task: one line of code, one
  61. |> blank line, two gratuitous ornaments, and three lines of loquacious
  62. |> description in which the real point ("skip the next widget") comes late in
  63. |> the sentence.
  64.  
  65. |> Realizing that we're taking code out of context, but I think I understand
  66. |> what Bryant is showing us here.  How about:
  67. |>     i++;        /* done with widget i; skip the one after it */
  68. |> The comment is still too wordy, but at least it doesn't take up 10% of
  69. |> the current on-screen space to describe a single integer increment.
  70.  
  71. And of course, if the next line is the closing brace of the body of
  72. the for-loop, the comment "done with widget i" is superfluous, so just
  73. "skip the next one" should be adequate.
  74.  
  75. One thing I haven't seen in this thread, however, is the importance of
  76. commenting *why* you think the code works.  In all but the simplest
  77. loops, for example, it is essential to state the loop invariants at
  78. the top of the loop.  If this is obvious from the code, so much the
  79. better, but in most cases it won't be.
  80.  
  81. Again, if you leave out a test for some specific condition because you
  82. are convinced that that condition can never occur, some comment as to
  83. *why* you are convinced is in order.
  84.  
  85. And any time you use something other than the obvious solution, then
  86. some comment as to why it wasn't used is in order.  Note that in this
  87. case, you're actually commenting the code that *wasn't* written.
  88.  
  89. I never cease to be amazed at the amount of code I see without such
  90. comments.  In some cases, I know personally that the programmer has
  91. though these things out, but you wouldn't guess it from their
  92. comments.
  93.  
  94. --
  95. James Kanze                             email: kanze@us-es.sel.de
  96. GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
  97. Conseils en informatique industrielle --
  98.                    -- Beratung in industrieller Datenverarbeitung
  99.