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