home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 137_01 / feb84col.ddj < prev    next >
Text File  |  1979-12-31  |  20KB  |  535 lines

  1. .pl 60
  2. .po 0
  3. ..
  4. .. "The C/Unix Programmer's Notebook"
  5. ..
  6. .. DDJ Column #2        as of 19-Sep-83
  7. .. (C) 1983 Anthony Skjellum.   All rights reserved.
  8. .. For publication in DDJ
  9. ..
  10. ..
  11. .. file (DDJCOL2.WS)        created:  24-Nov-83
  12. ..                updated:  24-Nov-83
  13. ..                updated:  25-Nov-83
  14. ..
  15. ..                completed: 25-Nov-83
  16. ..
  17. .he "C/Unix Programmer's Notebook" by Anthony Skjellum. (C) 1983. For 2/84 DDJ.
  18.  
  19.                                Introduction
  20.  
  21. ..
  22.     In the first installment of this column,  I proposed a standard for 
  23.  
  24. the  layout  of C code (see DDJ #84,  October,  1983.)  Significant  reader 
  25.  
  26. response  was received concerning this subject.   The vast majority of  the 
  27.  
  28. letters received were in favor of the concept of a C layout  standard.   In 
  29.  
  30. this column, I will present reader comments concerning the layout standard, 
  31.  
  32. and discuss some modifications and additions to the proposed standard based 
  33.  
  34. on reader suggestions.  
  35.  
  36. ..
  37.     Some  comments  were  also  received concerning  my  discussion  of 
  38.  
  39. runtime library and linkage format incompatibilities.   Discussion of these 
  40.  
  41. points will be left for a future column.
  42.  
  43.                   Modifying the Proposed Layout Standard
  44.  
  45.                         I. Questions of White Space 
  46.  
  47. ..
  48.     Several readers took exception to a particular point concerning the 
  49.  
  50. proposed layout standard.   This was point 4f. which states "No white space 
  51.  
  52. character  is  placed between a keyword (e.g.  if)  and  its  parenthesized 
  53.  
  54. argument."  David D. Clark of State College, Pennsylvania, writes:
  55.  
  56. ..
  57.     "In  general,  I like your coding standard  suggestions.   My  only 
  58. strong  objection  is your idea to leave out spaces between reserved  words 
  59. [and their arguments].  It makes them look like function invocations."
  60.  
  61.     Tim Smith of Evanston, Illinois, notes:
  62.  
  63. ..
  64.     "...I  think  that a single space between a function name  and  the 
  65. initial opening parenthesis, or after "if's" and "elses", looks better..."
  66.  
  67.     Guy Scharf of Mountainview, California writes:    
  68.  
  69. ..
  70.     "4f.   I  have a strong preference to always put a space between  a 
  71. reserved word (e.g.  if,  while) and its parenthesized argument.  This adds 
  72. legibility for me."
  73.  
  74.     Finally, Charlie Brady of New South Wales, Australia, writes:
  75.  
  76. ..
  77.     "The  only real beef I have with you is the formatting of  keywords 
  78. and  their parenthetical expressions.   I can see no reason to depart  from 
  79. Kernighan  and  Ritchie  on  this  point,  and  a  number  of  reasons  for 
  80. maintaining  their  convention.   Firstly,  a  flow  control  construct  is 
  81. semantically distinct from a function call,  and a formatting difference is 
  82. a  reasonable  way  of  distinguishing  them.    Secondly,  the  formatting 
  83. difference  simplifies  the  use  of  a  text  editor  for  such  tasks  as 
  84. constructing structure charts.   Third, your recommendation departs from at 
  85. least  three  extant recommended standards,  namely Kernighan and  Ritchie, 
  86. Thomas  Plum  (C Standards and Guidelines,  Plum-Hall 1981)  and  Tim  Lang 
  87. (Formatting C,  AUUGN Vol 4,  No 1, Jan 1982. Enclosed)."  (I want to thank 
  88. Mr.  Brady for including a copy of the Lang article with his letter.  The C 
  89. standard proposed there is very compatible with the one I have proposed.)  
  90.  
  91. ..
  92.     After considering the above remarks,  I have come to the conclusion 
  93.  
  94. that  the space really does serve a useful purpose.   Therefore,  I suggest 
  95.  
  96. that  point 4f.  should be changed to read:  "A single white  character  is 
  97.  
  98. (optionally)  placed  between  a keyword (e.g.  if) and  its  parenthesized 
  99.  
  100. argument."  (Making the white space character optional is another point for 
  101.  
  102. debate.)  I think it should be optional,  but recommended.   I don't  think 
  103.  
  104. that  adding a space for function call invocations would be beneficial,  as 
  105.  
  106. suggested by Mr. Smith. 
  107.  
  108. ..
  109.     Another   question  concerning  white  space  insertion  comes   in 
  110.  
  111. connection with argument lists.  The original standard does not indicate if 
  112.  
  113. spaces  should  be  included.   It is my opinion that  a  comma  should  be 
  114.  
  115. directly adjacent to the argument that it follows,  and that a single white 
  116.  
  117. space should follow each comma to add legibility.  I am also convinced that 
  118.  
  119. parentheses  should be adjacent to the argument(s) they enclose.   Thus (in 
  120.  
  121. agreement with Tim Lang's article mentioned above) I would write:
  122.         
  123.         x = atan(sin(y));        
  124.  
  125. and not
  126.  
  127.         x = atan(  sin( y ) );  
  128.  
  129. ..
  130.     Yet another point not previously mentioned is that binary operators 
  131.  
  132. should be delimited by white space.   Thus,  the following statement  lacks 
  133.  
  134. sufficient white space:
  135.  
  136.         v = sin(ln(1.0+x));
  137.  
  138. while this expression is properly formed:
  139.  
  140.         v = sin(ln(1.0 + x));
  141.  
  142. ..
  143.     Finally,  section  4  needs  to  be  updated  to  include  a  style 
  144.  
  145. specification for pointer references.   I think that that operators '.' and 
  146.  
  147. '->'  should not be delimited by spaces from the objects which they act on.  
  148.  
  149. This point and the three above are formalized in Figure I.  as additions to 
  150.  
  151. section 4.
  152.  
  153. .pa
  154.               ------------------FIGURE I.--------------------
  155.  
  156.     (Under section 4)
  157.  
  158.          b.  Binary operators (e.g.  +,  -, / but not '->' and '.') and 
  159.                  assignment operators (eg =, *=, &=) are delimited by white 
  160.                  space.
  161.  
  162.          g.  Parentheses  should be adjacent to the  argument(s)  which 
  163.                  they enclose.  
  164.  
  165.          h.  A comma is bound to the argument which preceeds and should 
  166.                  be followed by a single space.
  167.                  
  168.  
  169.          i.  Operators   such  as  '->'  and  '.'  (used   in   pointer 
  170.                  references)  directly  bind  to their  arguments  with  no 
  171.                  intervening spaces.
  172.  
  173.           ---------------------END FIGURE I.--------------------         
  174.  
  175.  
  176. ..
  177.     The  Lang article points out a circumstance under which  point  9g. 
  178.  
  179. need  not  be  followed.   This occurs when  very  complicated  conditional 
  180.  
  181. expressions of the form "keyword (expr)" are split over several lines.  For 
  182.  
  183. example,  instead  of using a crowded expression as illustrated in  Example 
  184.  
  185. 1a.,  a  more  readable  form is selected (see Example 1b.)  Note  that  in 
  186.  
  187. Example 1b.  that the parentheses are placed on lines by themselves,  since 
  188.  
  189. they  bracket  a  multiline  expression,   much  like  braces  enclose  the 
  190.  
  191. statements of a block.
  192.  
  193.            --------------------Example Ia.---------------------
  194.  
  195.         if((a  == 1) && (b == 2) && (c == 3) && ((d == 4) || (d == 5)))
  196.         {
  197.             /* operations performed if conditional true */
  198.             ...
  199.         }
  200.  
  201.           --------------------End Example Ia.--------------------
  202.  
  203.            --------------------Example Ib.---------------------
  204.  
  205.         if    /* we make multi-line expression look like a block */
  206.         (
  207.             (a == 1) &&
  208.             (b == 2) &&
  209.             (c == 3) &&
  210.             ((d == 4) || (d == 5))
  211.         )
  212.         {
  213.             /* operations performed if conditional true */
  214.             ...
  215.         }
  216.  
  217.           --------------------End Example Ib.--------------------
  218.  
  219. ..
  220.     Another  point  of  minor  objection  was  the  tabulation   method 
  221.  
  222. specified  by  the  standard (point 1a.).   Steve Newberry  of  Los  Altos, 
  223.  
  224. California, states:
  225.  
  226. ..
  227.     "Upon one point I do feel compelled to argue with you,  and that is 
  228. the  tab convention:   The DEPTH of the tab stop on a given page is of  far 
  229. less  significance to the readability of that page than is the  CONSISTENCY 
  230. of  the depth.   I REALLY don't want to use different size tabs on the same 
  231. page."  
  232.     
  233.     Tim Smith writes:
  234.  
  235.     "I personally follow most all of his suggestions on how to actually 
  236. format the code on the line and page,  with only two exceptions.   I ALWAYS 
  237. use 4-space tab stops..."  
  238.  
  239. ..
  240.     I  agree  that  having a single tab size is the preferable  way  to 
  241.  
  242. write  C  code.   Standard tabs give more openness to the  code,  and  make 
  243.  
  244. various  parts  of a program easier to pick out.   My rationale  for  large 
  245.  
  246. horizontal  tabbing  is  the same as for  vertical  tabbing.   I  want  the 
  247.  
  248. program's  significant  portions to stand out.  However,  I propose  adding 
  249.  
  250. point 1b.  to the standard:  "4-space tabs may be used in lieu of  standard 
  251.  
  252. tabs  in  cases  where a sub-program includes highly nested  segments."   I 
  253.  
  254. would  also  include point 1c.:  "Only one of the two  tabbing  conventions 
  255.  
  256. should be employed in any given program module."
  257.  
  258. ..
  259.     Some  currently available screen editors provide a  feature  called 
  260.  
  261. horizontal scrolling.   With horizontal scrolling,  the user views a window 
  262.  
  263. of  his file in both the vertical and horizontal directions.   Thus,  files 
  264.  
  265. with  lines  longer than the display device may be  handled  intelligently.  
  266.  
  267. Under such circumstances,  there is no real disadvantage to using  standard 
  268.  
  269. tabs to any desired nesting depth,  which is permitted by point 1a.  of the 
  270.  
  271. proposed standard.  
  272.                           II. Other corrections.
  273.  
  274. ..
  275.     Charlie  Brady pointed out an unnecessary point in section 3 (point 
  276.  
  277. 3e) which stated:  "When a null block is used (e.g. "{}"), it may appear on 
  278.  
  279. the same line as other statements (e.g. do {} while(expr);).  He writes:
  280.  
  281. ..
  282.     "Another  minor point of disagreement concerns the use of the  null 
  283. block ({}).  This is never necessary, and I believe that the null statement 
  284. (;) is clearer.  It should be emphasised that the null statement deserves a 
  285. line of its own.  Your example:
  286.  
  287.         do {} while(expr);
  288.  
  289. is more simply written
  290.  
  291.         while (expr)
  292.             ;
  293.  
  294. ..
  295.     In accordance with Mr. Brady's remark, I propose replacing point 3e 
  296.  
  297. with the following:   "The null block ({}) can always be avoided.   Instead 
  298.  
  299. of a null block,  use the null statement."  I also would add point  3f#ii.: 
  300.  
  301. "The null statement is always on a line by itself."
  302.  
  303.  
  304.                        III. Documentation Standards
  305.  
  306. ..
  307.     In  addition to a code layout standard,  James Halstead of  Joliet, 
  308.  
  309. Illinois, has proposed a basic documentation standard.  He writes:
  310.  
  311. ..
  312.     "...I strongly suggest that the original standards one through nine 
  313. be  renumbered two through ten so that the first and foremost standard  may 
  314. be  inserted."   The documentation standard suggested by  Mr.  Halstead  is 
  315. presented  in Figure II.  and I think it should be included in my  proposed 
  316. standard.   However, I have decided to number the documentation standard as 
  317. section  0.  in  order to preserve the numbering of the  current  sections.  
  318. (Slight  additions  which  I  made  include  an  asterisk  to  indicate  my 
  319. addition.)
  320.  
  321. .pa
  322.              -------------------FIGURE II.-------------------
  323.                   
  324.     0.   Identification  Description (I.D.) information must appear  at 
  325.          the beginning of each C language source file.
  326.  
  327.          a.  The recommended format is:
  328.  
  329.                   i.    Begin comment (/*). 
  330.                   ii.   Space.
  331.                  iii.  Title (identification name normally = filename).
  332.           *  iv.   Sub-title (ie what program system does this file
  333.                                     belong to).
  334.                  v.    Space.
  335.                  vi.   Classification (see below).
  336.                  vii.  Year.
  337.                  viii. Owner.
  338.                  ix.   Status (see below).
  339.               *  x.    Current Version number and brief history.    
  340.                  xi.   Date.
  341.                  xii.  Functional/Structural Description in brief.
  342.           *  xiii. Portability synopsis.
  343.           *     xiv.  Space.
  344.                  xv.   End Comment (*/).
  345.               *  xvi.  Space.
  346.  
  347.          b.  The program classification (vi.) is one of the following:
  348.  
  349.                  i.    public-domain.
  350.                  ii.   copyright.
  351.               *  iii.  copyright: released for non-commercial purposes.
  352.                  iv.   unclassified.
  353.                  v.    secret.
  354.                  vi.   no classification.
  355.  
  356.          c.  The program status (ix.) is one of the following:
  357.  
  358.                  i.    outline.
  359.                  ii.   draft.
  360.          iii.  test (alpha, beta etc.)
  361.                  iv.   release.
  362.  
  363.            -------------------END FIGURE II.--------------------
  364.  
  365. .pa
  366. ..
  367.     In  addition to Section 0.,  I think a basic documentation standard 
  368.  
  369. is in order for functions as well.   Such a standard is presented in Figure 
  370.  
  371. III.   I  have  placed this under section 10,  since I  think  of  function 
  372.  
  373. documentation  is a separate task from module documentation as described in 
  374.  
  375. section 0.
  376.  
  377.              -------------------FIGURE III.-------------------
  378.          
  379.     10.  Each   function   should   contain   the   following   minimum 
  380.              documentation: 
  381.  
  382.              a.   A general explanation of the function performed.
  383.              b.   Its  name,  and a description of its arguments  including 
  384.                   their types, and legal values.
  385.              c.   A description of the functional return value, if any.
  386.              d.   A list of non-standard functions used by the function.
  387.              e.   A list of external variables used and/or modified by
  388.                   the function.
  389.          f.   A description of the error handling characteristics of the
  390.             function.
  391.              g.   A valid calling sequence example, if practical.
  392.  
  393.           -------------------END FIGURE III.--------------------
  394.  
  395.  
  396.                             IV. Other proposals
  397.  
  398. ..
  399.     Several other readers made suggestions for the standard.   I  think 
  400.  
  401. that Tim Smith proposes several which merit discussion.  They are presented 
  402.  
  403. here with the point numbers they receive as part of the standard:
  404.  
  405.     (5c).  Don't  nest  comments,  even if  your  preprocessor/compiler 
  406.                allows it.  
  407.  
  408.     (6f).  If there are many declarations, whether on one line or many, 
  409.                alphabetize them.
  410.  
  411.     (9g).  Restrict  variable  and  function  names  to  7  well-chosen 
  412.                characters, even if your compiler allows more.
  413.  
  414.     Steve Newberry writes the following about standards:
  415.  
  416. ..
  417.     "I   applaud  your  interest  in  establishing  a  standard  format 
  418. convention for C programs.  However I feel that your effort would have more 
  419. impact  if tied to support of Tom Plum's book C Programming  Standards  and 
  420. Guidelines, Version U (Unix and offspring), Edition 3: Jan. '82.  Presented 
  421. in  this  manner,  your  proposed formatting standard would be  seen  as  a 
  422. consistent  extension  of a more general set of standards already  in  wide 
  423. circulation."
  424.  
  425.                            Other points of View
  426.  
  427.     Although  most  readers  were favorable to the idea of a  C  format 
  428.  
  429. standard, Douglas M. Potter of Seattle, Washington, writes:
  430.  
  431.     "I afraid I don't see much advantage of your proposed standard over 
  432. theirs [Kernighan and Ritchie].   In both cases,  the size of the ident  is 
  433. too  large,  I  always run out of room on the right side with a  tab  sized 
  434. ident.  I also find that nobody uses enough white space."
  435.  
  436.     John F. Draffen of Texas City, Texas, wrote me a detailed letter on 
  437.  
  438. why he didn't like the idea.  He writes:
  439.  
  440.     "I  am writing to express my objections...In the first place  I  do 
  441. not  think a standard of this type is either necessary or  desirable.   The 
  442. layout  has  nothing  to do with portability which to my mind is  the  only 
  443. excuse for a standard.  It seems to me that it is hard enough to get people 
  444. to agree on necessary standards."
  445.  
  446.     "In  the second place I do not agree with many of your  suggestions 
  447. on  style.   One of the nice things about C that it shares with Fortran  is 
  448. it's  relative  conciseness.   I  do  not  like  to  see  code  strung  out 
  449. unnecessarily.  C does not interject unnecessary constructions, and I think 
  450. that  introducing  unnecessary  white  space,   excessive  indentation  and 
  451. meaningless comments is a kind of gingerbread that we can do without."  Mr. 
  452. Draffen's style philosophy is listed in Figure IV.  
  453.     
  454.             --------------------FIGURE IV.--------------------
  455.  
  456.     1.  Punctuation  should  be  used  sparingly.    The  insertion  of 
  457.             unnecessary white space should be avoided.
  458.  
  459.     2.  Block  structure  should  be  used  indicated  by  indentation.  
  460.             Excessive indentation should be avoided.
  461.  
  462.         3.  Comments and program code should be separated.  Comments on the 
  463.             same  line as code should be displaced far enough to the  right 
  464.             that they do not obscure the code.
  465.  
  466.         4.  Comments  should be meaningful.   Comments that do no more than 
  467.             repeat  what  has  already  been said by  the  code  should  be 
  468.             avoided.
  469.  
  470.          ----------------------END FIGURE IV.---------------------
  471.  
  472.     I don't really agree with Mr. Draffen on several counts.  First and 
  473.  
  474. foremost  is  that  portability is not the sole subject  of  importance  in 
  475.  
  476. programming.  The ability to maintain, understand, correct and enhance code 
  477.  
  478. is of great importance.   To understand someone else's code,  (or your  own 
  479.  
  480. code  at a later date) requires some degree of formatting.   Comments which 
  481.  
  482. seem  less than essential to the programmer must sometimes be included  for 
  483.  
  484. the sake of others.   This is immensely important.   It is often  difficult 
  485.  
  486. for a programmer to know how to comment his code, since he usually can  not 
  487.  
  488. know  the  level of sophistication of later readers.   Thus,  it  is  often 
  489.  
  490. better to include a few extra comments, than to comment code sparsely.  
  491.  
  492.     I  suggested in my previous column that users should maintain their 
  493.  
  494. own code in the form that they prefer.  However, code distributed to others 
  495.  
  496. could  (and should) meet some minimum standard of neatness (ie  formatting) 
  497.  
  498. and  presentation.   Some of this can be provided by a beautifier but  most 
  499.  
  500. must be done by the programmer himself.
  501.  
  502.     As  one  final  note  on C  layout,  I'm  including  an  insightful 
  503.  
  504. paragraph which Tim Smith included his letter.   It suggests why so much  C 
  505.  
  506. code is so poorly formatted and commented:
  507.  
  508.     "I  don't think,  however,  that you will ever get Unix wizards  to 
  509. follow these recommendations.  I should have noted that I use Skjellum-like 
  510. conventions   when   I'm  writing  micro-based  applications.    When   I'm 
  511. maintaining Unix sources I stick with the standard Unix conventions,  which 
  512. are pretty much K & R [Kernighan and Ritchie] standard.   Unix whiz's think 
  513. that aligning curly braces is irrelevant,  since "vi", the editor which 90% 
  514. of them use, will always let you find the top or bottom match for any brace 
  515. automatically.   Also, and probably more important, Unix system guys always 
  516. debug  by staring at their CRT's,  never from printouts (that's  for  COBOL 
  517. programmers),  and the goal is to reduce the number of lines of a function, 
  518. so  that  as much of it as possible will fit on a screen.   Seeing a  WHOLE 
  519. LINE taken up by just an opening brace must drive them whacko,  and some of 
  520. them will even close blocks on the end of a line of code (yecch!)"
  521.  
  522.                                 Conclusion
  523.  
  524. ..
  525.     In  this column,  I have presented additions and corrections to the 
  526.  
  527. proposed layout standard,  based on reader response.  I have also presented 
  528.  
  529. the opposing point of view.  Most of the letters received were positive, so 
  530.  
  531. it was difficult to include more dissenting remarks.  I want to thank those 
  532.  
  533. who sent in their comments about the standard.
  534.  
  535.