home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16038 < prev    next >
Encoding:
Internet Message Format  |  1992-11-06  |  12.4 KB

  1. Path: sparky!uunet!portal!lll-winken!framsparc.ocf.llnl.gov!booloo
  2. From: booloo@framsparc.ocf.llnl.gov (Mark Boolootian)
  3. Newsgroups: comp.lang.c
  4. Subject: The Correct Way To Write C if-Statements
  5. Message-ID: <140742@lll-winken.LLNL.GOV>
  6. Date: 5 Nov 92 22:49:33 GMT
  7. Sender: usenet@lll-winken.LLNL.GOV
  8. Organization: Lawrence Livermore National Laboratory
  9. Lines: 280
  10. Nntp-Posting-Host: framsparc.ocf.llnl.gov
  11.  
  12.  
  13. The following piece was written by Jim Minton, an aspiring C programmer.
  14. I convinced him to let me post it to the net.  Happy reading and flames
  15. to /nev/dull...
  16.  
  17.  
  18.  
  19.         The Correct Way To Write C if-Statements
  20.                 by
  21.                 Jim Minton
  22.  
  23.  
  24.  
  25. Introduction
  26.  
  27. This report is the result of a single meeting held by a committee of 
  28. people dedicated to wrenching whatever sanity can be found from the 
  29. chaotic mess known as the C language.  At the first meeting, which 
  30. was held in secret, the two people on the committee came to an 
  31. agreement on how C if-statements should be constructed.  At future 
  32. meetings other C statements will be discussed and defined.  As these 
  33. meetings take place and decisions are made, reports such as this 
  34. will be generated.  At the first meeting the possibility of adding 
  35. more members to the committee was discussed, but this proposal was 
  36. voted down because it was felt that other members might have 
  37. opinions different from those held by the current members and would 
  38. therefore make it much more difficult to come to a consensus.  The 
  39. current committee has had enough trouble hammering out these hard, 
  40. but necessary decisions.
  41.  
  42. It should be noted that all decisions in this and following papers 
  43. are fully binding and everyone is expected to immediately adhere to 
  44. the policies and suggestions outlined herein.
  45.  
  46.  
  47. The Committee
  48.  
  49. By some extraordinarily fortunate quirk of nature the committee was 
  50. made up of a rare group of extraordinary individuals who are blessed 
  51. with uncommon insight and intelligence.  These individuals possess 
  52. no biases or prejudices.  It is a rare moment in history when 
  53. exactly the right people are gathered together at the right moment 
  54. to solve a great and pressing problem, but such a moment did occur 
  55. and we are now able and pleased to share the results.
  56.  
  57.  
  58. Problem Description
  59.  
  60. Let us begin with a brief technical description.  The C language has 
  61. been designed so that there are several thousand possible ways to 
  62. write any simple if-statement.  This allows great flexibility and 
  63. insures that no two programmers anywhere in the universe will ever 
  64. write an if-statement that looks like any other.  As can quickly be 
  65. seen, however, this sometimes makes it troublesome to read Code 
  66. Recorded by Another Programmer (CRAP)[1].  Of course there never has 
  67. been any problem reading one's own code and this note would not be 
  68. necessary if all we ever had to read was our own code.  However, 
  69. (Sigh), it is very often the case that we have to read CRAP[2].
  70.  
  71. An important point to remember, and one we hope is strongly 
  72. reinforced by reading CRAP, is that when other people read our code, 
  73. it is CRAP to them!  Now here, you see, we have an opportunity to do 
  74. good-when producing CRAP for other people we can either show our 
  75. baser meaner natures and try to get even, or we can rise above such 
  76. pettiness and try to produce something that others will refer to as 
  77. good CRAP.  We all should, in fact, strive to make others refer to 
  78. our code as "good CRAP."  This is in fact the origin of the phrase, 
  79. "There's nothing like a good CRAP," which should, and probably will, 
  80. become the motto of our Department.
  81.  
  82. But let us return to the subject of this paper which is The Correct 
  83. Way To Write C if-statements.  As previously stated there are many 
  84. ways an if-statement can be written.  This is in fact the problem.  
  85. If there were only one way to write an if-statement then we would 
  86. all write them that way and each of us would be able to easily and 
  87. quickly recognize them.  Now, if it is the case that we could 
  88. quickly and easily recognize them, if they were all written the same 
  89. way, how about a novel proposal-we propose that we all write them 
  90. the same way!  Further, we propose that we do this despite our 
  91. natural inclination to do otherwise!
  92.  
  93.  
  94. The Comma Operator
  95.  
  96. Before proceeding with the following discussions we must first state 
  97. our policy regarding the comma operator.  The comma operator is not 
  98. allowed. The comma operator is forbidden.  The comma operator must 
  99. never be used.  The comma operator should be removed from the 
  100. language.  The comma operator is an abomination.  The comma operator 
  101. has produced more confusion than even the most gifted politician.  
  102. We hope everyone understands our feelings about  the comma operator.
  103.  
  104.  
  105. White Space
  106.  
  107. Some people are so stingy with white space we can only wonder if 
  108. they live in a cave with no lights and prefer dark and gloomy 
  109. things.  Are these people trying to save paper?  Since most code is 
  110. viewed on some sort of electronic monitor, saving paper seems to be 
  111. a strange goal.  And even if the code is looked at on paper, how 
  112. much paper is being saved anyway?  Are the savings worth the 
  113. obfuscation and lack of readability that's caused?  The answer is 
  114. simply, "No."  We loudly proclaim that writing readable code should 
  115. be everyone's primary goal.  Remember that we are trying to make 
  116. people refer to our code as good CRAP.  Effective use of white space 
  117. can help achieve this goal.  There is absolutely nothing wrong with 
  118. using plenty of white space and in fact it should be used in 
  119. abundance.
  120.  
  121. "But I can't see the code."  Have you ever heard anyone use this 
  122. statement as an objection to white space and/or code comments?  I 
  123. think we all have.  These people are to be shunned and avoided as if 
  124. they were lepers.  If they produce code for you this code will be 
  125. unreadable and undocumented.  The only one who will be able to read 
  126. it will be the author who "wants to see the code."  All other people 
  127. will refer to this code as bad CRAP.  People who "want to see the 
  128. code" can single-handedly ruin an entire project.  All of the code 
  129. produced by these people will have to be rewritten.  If you ever 
  130. hear someone mouth this phrase you should immediately notify your 
  131. supervisor and seek purification by spreading several pounds of 
  132. white space around your office.
  133.  
  134.  
  135. Indenting if-statements
  136.  
  137. As do most programmers, we feel that the body (the statements 
  138. following the if) of an if-statement should be indented.  We realize 
  139. there are some die-hard, pinko-perverts out there who, looking for 
  140. job security and trying to win obfuscated C contests, won't indent 
  141. anything.  If you are not a member of this latter group then you 
  142. undoubtedly agree with us and the only thing left to decide is, "How 
  143. far to indent?"
  144.  
  145. People who indent only 1 space might as well not indent at all.  No 
  146. one can tell they are trying to indent and it looks as if they are 
  147. randomly starting lines in columns near and about column 1.  "1" is 
  148. out.  Indenting 2 spaces is a possibility, however 2 is awfully 
  149. close to 1.  "2" is out.  "3" is not a power of 2.  "3" is out.  
  150. "4", Ah Hah!  "4" is a power of two and is also the number of 
  151. fingers on your right hand!  It seems as though nature itself has 
  152. selected an indentation level of "4!"  Numbers greater than 4 are 
  153. either not a power of 2 or are so large that just a few indentations 
  154. immediately force us to (or past) the right margin.  All numbers 
  155. greater than 4 are out.
  156.  
  157. The indentation level is 4 spaces!
  158.  
  159.  
  160. Braces
  161.  
  162. Braces belong on children's teeth and around the body of every 
  163. single if-statement.  There are no exceptions.  Every single if-body 
  164. is to be surrounded by braces.  The essence of the argument in favor 
  165. of braces is this: all if-statements should look the same, 
  166. therefore, if some if-statements have braces (as all with multi-
  167. statement bodies do) and others (those with single statement bodies) 
  168. don't, then it is impossible for if-statements to look the same.  
  169. Either they all have braces or none of them have braces.  Since the 
  170. language requires that some if-statements have braces, then, to make 
  171. them all look the same, we have to put braces around every single 
  172. if-body, even bodies that have only one statement!
  173.  
  174. Another argument in favor of braces is the fact that braces "open 
  175. the code up" and allow it to be more easily seen and read.  For 
  176. example compare the following two if-statements:
  177.  
  178.     if(ExampleOfBunchedUpCode())
  179.             UnReadable=Congested+Constipated+AnalRetentiveCode;
  180.  
  181.  
  182.     if ( OpenCode )
  183.     {
  184.          APleasureToRead = Attractive = Airy + Open + Breezy;
  185.     }
  186.  
  187. The first example is bunchedupandhardtoread.  The second example, 
  188. through the use of braces, is open, airy, and easy to examine and 
  189. read.  The use of braces forces the use of white space which 
  190. naturally makes things easier to read.
  191.  
  192. If braces are always guaranteed then we never have to wonder if the 
  193. line following an if is part of the if-expression or is perhaps an 
  194. if-body containing only one statement.  The following example:
  195.  
  196.     if (((Error = Sub1(Abc, Sub2(XYZ), PDQ), < 0))  || (
  197.             a + b + c <= 75                               ))
  198.  
  199. illustrates our point.  No one will be fooled or confused while 
  200. making a quick visual scan of the preceding if-statement if they 
  201. know that all if-statement bodies are surrounded by braces.  The 
  202. preceding sentence contains a point that is so important it must be 
  203. explicitly emphasized-when reading CRAP we always try to make a 
  204. "quick visual scan."  If CRAP is properly written we will never be 
  205. fooled and it must always be our goal to never fool anyone else.
  206.  
  207.  
  208. Where To Place The Braces
  209.  
  210. Now that we agree to always have braces we must decide where to put 
  211. them.  There are some people who like to put the opening left brace 
  212. way over out of sight to the right of the if-statement.  We can only 
  213. suppose they are ashamed of the brace or are purposefully trying to 
  214. produce code that others would consider bad CRAP.  The placement of 
  215. the opening left brace is critical; with it you can inform the reader 
  216. where the if-block will end.  The if-block will end with a closing 
  217. right brace that is indented at exactly the same level.  The braces 
  218. can be used to "sight along."  Properly placed they can make CRAP 
  219. easy to read and scan.
  220.  
  221.     if ( WeUseBraces() )
  222.     {
  223.             Properly = WeCan + SightAlong + Them;
  224.     }
  225.  
  226. In the preceding examples you may have noticed that no other 
  227. characters have been placed on a line that contains a brace.  It is 
  228. always the case that no other characters are allowed on a line 
  229. containing an opening left brace.  However it is allowed and 
  230. considered good form to put a comment on the closing right brace as 
  231. in the following example:
  232.  
  233.     if ( AnIfBlock )
  234.     {
  235.             HasManyStatements(0);
  236.         .
  237.         .
  238.             The = Last + Statement + Fini;
  239.     }  /* End of ( AnIfBlock ) test. */
  240.  
  241. If comments were allowed on the opening left brace the code would 
  242. begin to appear dark and dense and foreboding.  Since code must 
  243. always appear open, airy, and inviting, comments on the opening left 
  244. brace are not allowed.
  245.  
  246.  
  247. else-Statements
  248.  
  249. Almost all of the arguments that apply to if-statements also apply 
  250. to else-statements.  A properly formed if-statement which contains 
  251. an else-clause follows:
  252.  
  253.     if ( SomeDarnThing == TRUE )
  254.     {
  255.             /*
  256.             *  A very informative comment.
  257.             */
  258.             WeDoTheTRUEThing( Abc );
  259.     }
  260.     else
  261.     {
  262.             WeDoTheFALSEThing( Abc );
  263.     }  /* End of ( SomeDarnThing == TRUE ) test. */
  264.  
  265. Notice the comment that follows the last right brace.  Comments are 
  266. only allowed on the last right brace.  It would, for example, be 
  267. highly improper to put a comment after the right brace that precedes 
  268. the else-statement.  It is important in the preceding example to 
  269. notice that it is open, airy, and easy-to-read.
  270.  
  271. Always remember our motto:  There's nothing like a good CRAP!
  272.  
  273.  
  274.  
  275. NOTES
  276.  
  277. [1] Years ago Sam Mendicino observed that every time someone picks up someone 
  278.     else's code the first thing they say is that the code is CRAP.  
  279.     Unfortunately, at that time, there was some misunderstanding of the
  280.     intent of this statement.  Some people mistakenly thought Sam was saying 
  281.     the code was crap.  This was of course ridiculous!  Sam was merely pointing 
  282.     out the obvious fact that this was Code Recorded (or written) by Another 
  283.     Programmer or just simply "CRAP."
  284.  
  285. [2] The original acronym was "CWAP" (for Code Written by Another Programmer).  
  286.     Unfortunately this acronym makes the speaker sound like Porky Pig, as in, 
  287.     "You Wascally Wabbit."  To avoid having world leading scientists at a large 
  288.     prestigious laboratory sound like Porky Pig, the acronym was changed to 
  289.     CRAP.
  290. -- 
  291. Mark Boolootian        booloo@llnl.gov        +1 510 423 1948
  292.