home *** CD-ROM | disk | FTP | other *** search
/ ftp.cse.unsw.edu.au / 2014.06.ftp.cse.unsw.edu.au.tar / ftp.cse.unsw.edu.au / pub / doc / papers / misc / cs.toronto.edu:programming / ihstyle.doc < prev    next >
Encoding:
Text File  |  1992-10-18  |  48.3 KB  |  1,585 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.           IIIInnnnddddiiiiaaaannnn HHHHiiiillllllll CCCC SSSSttttyyyylllleeee aaaannnndddd CCCCooooddddiiiinnnngggg SSSSttttaaaannnnddddaaaarrrrddddssss
  11.             aaaassss aaaammmmeeeennnnddddeeeedddd ffffoooorrrr UUUU ooooffff TTTT ZZZZoooooooollllooooggggyyyy UUUUNNNNIIIIXXXX||||----
  12.  
  13.  
  14.                         L.W. Cannon
  15.                         R.A. Elliott
  16.                        L.W. Kirchhoff
  17.                         J.H. Miller
  18.                         J.M. Milner
  19.                          R.W. Mitze
  20.                          E.P. Schan
  21.                       N.O. Whittington
  22.  
  23.                          Bell Labs
  24.  
  25.  
  26.                        Henry Spencer
  27.  
  28.                   Zoology Computer Systems
  29.                    University of Toronto
  30.  
  31.  
  32.  
  33.                           _A_B_S_T_R_A_C_T
  34.  
  35.           This document is an annotated  (by  the  last
  36.      author)  version of the original paper of the same
  37.      title.  It describes a set of coding standards and
  38.      recommendations  which  are  local  standards  for
  39.      officially-supported UNIX programs.  The scope  is
  40.      coding style, not functional organization.
  41.  
  42.  
  43.  
  44. April 18, 1990
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. _________________________
  56. |- UNIX is a trademark of Bell Laboratories.
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.           IIIInnnnddddiiiiaaaannnn HHHHiiiillllllll CCCC SSSSttttyyyylllleeee aaaannnndddd CCCCooooddddiiiinnnngggg SSSSttttaaaannnnddddaaaarrrrddddssss
  77.             aaaassss aaaammmmeeeennnnddddeeeedddd ffffoooorrrr UUUU ooooffff TTTT ZZZZoooooooollllooooggggyyyy UUUUNNNNIIIIXXXX||||----
  78.  
  79.  
  80.                         L.W. Cannon
  81.                         R.A. Elliott
  82.                        L.W. Kirchhoff
  83.                         J.H. Miller
  84.                         J.M. Milner
  85.                          R.W. Mitze
  86.                          E.P. Schan
  87.                       N.O. Whittington
  88.  
  89.                          Bell Labs
  90.  
  91.  
  92.                        Henry Spencer
  93.  
  94.                   Zoology Computer Systems
  95.                    University of Toronto
  96.  
  97.  
  98.  
  99. _1.  _I_n_t_r_o_d_u_c_t_i_o_n
  100.  
  101.      This document is a result  of  a  committee  formed  at
  102. Indian  Hill  to  establish a common set of coding standards
  103. and recommendations for  the  Indian  Hill  community.   The
  104. scope  of  this work is the coding style, not the functional
  105. organization of programs.  The standards  in  this  document
  106. are not specific to ESS programming only1.  We have tried to
  107. combine previous work [1,6] on C style into a uniform set of
  108. standards that should be appropriate for any  project  using
  109. C2.
  110.  
  111. _________________________
  112. |- UNIX is a trademark of Bell Laboratories.
  113.  
  114. 1.   In  fact,  they're pretty good general standards.  ``To
  115.      be  clear  is  professional;  not  to   be   clear   is
  116.      unprofessional.''  -  Sir Ernest Gowers.  This document
  117.      is  presented  unadulterated;   U  of   T   variations,
  118.      comments, exceptions, etc. are presented in footnotes.
  119.  
  120. 2.   Of   necessity,   these   standards  cannot  cover  all
  121.      situations.  Experience and  informed  judgement  count
  122.      for  much.   Inexperienced  programmers  who  encounter
  123.      unusual situations should consult 1)  code  written  by
  124.      experienced  C programmers following these rules, or 2)
  125.      experienced C programmers.
  126.  
  127.  
  128.  
  129.  
  130.                        April 18, 1990
  131.  
  132.  
  133.  
  134.  
  135.  
  136.                            - 2 -
  137.  
  138.  
  139. _2.  _F_i_l_e _O_r_g_a_n_i_z_a_t_i_o_n
  140.  
  141.      A file consists of  various  sections  that  should  be
  142. separated by several blank lines.  Although there is no max-
  143. imum length requirement for source files,  files  with  more
  144. than about 1500 lines are cumbersome to deal with.  The edi-
  145. tor may not have enough temp space to edit the file,  compi-
  146. lations  will go slower, etc.  Since most of us use 300 baud
  147. terminals, entire rows of asterisks, for example, should  be
  148. discouraged3.  Also lines longer than  80  columns  are  not
  149. handled  well by all terminals and should be avoided if pos-
  150. sible4.
  151.  
  152.      The suggested order of sections for a file is  as  fol-
  153. lows:
  154.  
  155. 1.   Any header file includes should be the first  thing  in
  156.      the file.
  157.  
  158. 2.   Immediately after the includes5 should  be  a  prologue
  159.      that tells what is in that file.  A description of  the
  160.      purpose  of  the  objects in the files (whether they be
  161.      functions, external data declarations  or  definitions,
  162.      or  something  else)  is more useful than a list of the
  163.      object names.
  164.  
  165. 3.   Any typedefs and defines that apply to the  file  as  a
  166.      whole are next.
  167.  
  168. 4.   Next come the global (external) data declarations.   If
  169.      a  set of defines applies to a particular piece of glo-
  170.      bal data (such as a flags word), the defines should  be
  171.      immediately after the data declaration6.
  172.  
  173. 5.   The functions come last7.
  174. _________________________
  175.  
  176. 3.   This is not a problem at U of T, or most other sensible
  177.      places, but rows of asterisks are still annoying.
  178.  
  179. 4.   Excessively long lines which result from deep indenting
  180.      are often a symptom of poorly-organized code.
  181.  
  182. 5.   A  common  variation, in both Bell code and ours, is to
  183.      reverse the order of sections 1  and  2.   This  is  an
  184.      acceptable practice.
  185.  
  186. 6.   Such  defines should be indented to put the _d_e_f_i_n_es one
  187.      level deeper than the first keyword of the  declaration
  188.      to which they apply.
  189.  
  190. 7.   They should be in some sort of meaningful order.   Top-
  191.      down   is   generally  better  than  bottom-up,  and  a
  192.      ``breadth-first''  approach  (functions  on  a  similar
  193.  
  194.  
  195.  
  196.                        April 18, 1990
  197.  
  198.  
  199.  
  200.  
  201.  
  202.                            - 3 -
  203.  
  204.  
  205. _2._1.  _F_i_l_e _N_a_m_i_n_g _C_o_n_v_e_n_t_i_o_n_s
  206.  
  207.      UNIX requires certain suffix conventions for  names  of
  208. files to be processed by the _c_c command [5]8.  The following
  209. suffixes are required:
  210.  
  211. +o    C source file names must end in ._c
  212.  
  213. +o    Assembler source file names must end in ._s
  214.  
  215.      In addition the following conventions  are  universally
  216. followed:
  217.  
  218. +o    Relocatable object file names end in ._o
  219.  
  220. +o    Include header file names end in ._h 9 or ._d
  221.  
  222. +o    Ldp10 specification file names end in ._b
  223.  
  224. +o    Yacc source file names end in ._y
  225.  
  226. +o    Lex source file names end in ._l
  227.  
  228. _3.  _H_e_a_d_e_r _F_i_l_e_s
  229.  
  230.      Header files are files that are included in other files
  231. prior  to  compilation  by  the  C  preprocessor.   Some are
  232. defined at the system  level  like  _s_t_d_i_o._h  which  must  be
  233. included  by  any  program  using  the standard I/O library.
  234. Header files are also used to contain data declarations  and
  235. defines that are needed by more than one program11.   Header
  236. _________________________
  237.      level   of  abstraction  together)  is  preferred  over
  238.      depth-first (functions  defined  as  soon  as  possible
  239.      after  their  calls).  Considerable judgement is called
  240.      for here.  If defining large  numbers  of  essentially-
  241.      independent  utility  functions,  consider alphabetical
  242.      order.
  243.  
  244. 8.   In addition to the suffix conventions given here, it is
  245.      conventional to use `Makefile' (not `makefile') for the
  246.      control file for _m_a_k_e and `README' for a summary of the
  247.      contents of a directory or directory tree.
  248.  
  249. 9.   Preferred.   An  alternate  convention  that   may   be
  250.      preferable in multi-language environments is to use the
  251.      same suffix as an ordinary source  file  but  with  two
  252.      periods instead of one (e.g. ``foo..c'').
  253.  
  254. 10.  No idea what this is.
  255.  
  256. 11.  Don't use absolute pathnames for header files.  Use the
  257.      <_n_a_m_e> construction for getting them  from  a  standard
  258.      place,   or   define   them  relative  to  the  current
  259.  
  260.  
  261.  
  262.                        April 18, 1990
  263.  
  264.  
  265.  
  266.  
  267.  
  268.                            - 4 -
  269.  
  270.  
  271. files should be functionally organized,  i.e.,  declarations
  272. for  separate subsystems should be in separate header files.
  273. Also, if a set of declarations is likely to change when code
  274. is  ported  from  one machine to another, those declarations
  275. should be in a separate header file.
  276.  
  277.      Header files should not be nested.  Some  objects  like
  278. typedefs  and  initialized  data  definitions cannot be seen
  279. twice by the compiler in one compilation.  On non-UNIX  sys-
  280. tems this is also true of uninitialized declarations without
  281. the _e_x_t_e_r_n keyword12.  This can happen if include files  are
  282. nested and will cause the compilation to fail.
  283.  
  284. _4.  _E_x_t_e_r_n_a_l _D_e_c_l_a_r_a_t_i_o_n_s
  285.  
  286.      External declarations should begin in column  1.   Each
  287. declaration  should  be  on  a  separate  line.   A  comment
  288. describing the role of the object being declared  should  be
  289. included,  with  the  exception  that a list of defined con-
  290. stants do not need comments if the constant names are suffi-
  291. cient  documentation.  The comments should be tabbed so that
  292. they line up underneath each other13.  Use the tab character
  293. (CTRL I if your terminal doesn't have a separate key) rather
  294. than blanks.  For structure and union template declarations,
  295. each  element  should  be  alone  on  a  line with a comment
  296. describing it.  The opening brace ( { )  should  be  on  the
  297. same line as the structure tag, and the closing brace should
  298. be alone on a line in column 1, i.e.
  299.  
  300. struct boat {
  301.         int wllength;   /* water line length in feet */
  302.         int type;       /* see below */
  303.         long sarea;     /* sail area in square feet */
  304. };
  305. /*
  306.  * defines for boat.type14
  307.  */
  308. #define KETCH   1
  309. #define YAWL    2
  310. #define SLOOP   3
  311. #define SQRIG   4
  312. #define MOTOR   5
  313. _________________________
  314.      directory.  The ----IIII option of the C compiler is the best
  315.      way  to  handle  extensive  private libraries of header
  316.      files;  it permits reorganizing the directory structure
  317.      without having to alter source files.
  318.  
  319. 12.  It should be noted that declaring variables in a header
  320.      file  is often a poor idea.  Frequently it is a symptom
  321.      of poor partitioning of code between files.
  322.  
  323. 13.  So should the constant names and their defined values.
  324.  
  325.  
  326.  
  327.  
  328.                        April 18, 1990
  329.  
  330.  
  331.  
  332.  
  333.  
  334.                            - 5 -
  335.  
  336.  
  337. If an external variable  is  initialized15  the  equal  sign
  338. should not be omitted16.
  339.  
  340.         int x = 1;
  341.         char *msg = "message";
  342.         struct boat winner = {
  343.                 40,     /* water line length */
  344.                 YAWL,
  345.                 600     /* sail area */
  346.         };
  347.  
  348. 17
  349.  
  350. _5.  _C_o_m_m_e_n_t_s
  351.  
  352.      Comments that  describe  data  structures,  algorithms,
  353. etc., should be in block comment form with the opening /* in
  354. column one, a * in column 2  before  each  line  of  comment
  355. text18, and the closing */ in columns 2-3.
  356.  
  357. _________________________
  358.  
  359. 14.  These  defines  are  better   put   right   after   the
  360.      declaration  of  _t_y_p_e,  within  the _s_t_r_u_c_t declaration,
  361.      with enough tabs after # to  indent  _d_e_f_i_n_e  one  level
  362.      more than the structure member declarations.
  363.  
  364. 15.  Any variable whose initial value is important should be
  365.      _e_x_p_l_i_c_i_t_l_y initialized, or at the very least should  be
  366.      commented  to  indicate that C's default initialization
  367.      to 0 is being relied on.
  368.  
  369. 16.  The empty initializer, ``{}'', should  never  be  used.
  370.      Structure initializations should be fully parenthesized
  371.      with braces.  Constants used to initialize longs should
  372.      be explicitly long.
  373.  
  374. 17.  In any file which is part of a larger whole rather than
  375.      a self-contained program, maximum use should be made of
  376.      the _s_t_a_t_i_c keyword  to  make  functions  and  variables
  377.      local  to single files.  Variables in particular should
  378.      be accessible from other files only  when  there  is  a
  379.      clear  need that cannot be filled in another way.  Such
  380.      usages should  be  commented  to  make  it  clear  that
  381.      another  file's  variables  are being used; the comment
  382.      should name the other file.
  383.  
  384. 18.  Some   automated   program-analysis   packages   use  a
  385.      different character in this position as  a  marker  for
  386.      lines   with   specific   items   of  information.   In
  387.      particular, a  line  with  a  `-'  here  in  a  comment
  388.      preceding  a function is sometimes assumed to be a one-
  389.      line summary of the function's purpose.
  390.  
  391.  
  392.  
  393.  
  394.                        April 18, 1990
  395.  
  396.  
  397.  
  398.  
  399.  
  400.                            - 6 -
  401.  
  402.  
  403.  
  404. /*
  405.  *      Here is a block comment.
  406.  *      The comment text should be tabbed over19
  407.  *      and the opening /* and closing star-slash
  408.  *      should be alone on a line.
  409.  */
  410.  
  411.  
  412.      Note that _g_r_e_p ^.\* will catch all  block  comments  in
  413. the  file.   In some cases, block comments inside a function
  414. are appropriate, and they should be tabbed over to the  same
  415. tab  setting as the code that they describe.  Short comments
  416. may appear on a single line indented over to the tab setting
  417. of the code that follows.
  418.  
  419.         if (argc > 1) {
  420.                 /* Get input file from command line. */
  421.                 if (freopen(argv[1], "r", stdin) == NULL)
  422.                         error("can't open %s\n", argv[1]);
  423.         }
  424.  
  425.  
  426.      Very short comments may appear on the same line as  the
  427. code  they describe, but should be tabbed over far enough to
  428. separate them from the statements.  If more than  one  short
  429. comment appears in a block of code they should all be tabbed
  430. to the same tab setting.
  431.  
  432.         if (a == 2)
  433.                 return(TRUE);           /* special case */
  434.         else
  435.                 return(isprime(a));     /* works only for odd a */
  436.  
  437.  
  438. _6.  _F_u_n_c_t_i_o_n _D_e_c_l_a_r_a_t_i_o_n_s
  439.  
  440.      Each function should be preceded  by  a  block  comment
  441. prologue that gives the name and a short description of what
  442. the function does20.  If the function returns a  value,  the
  443. type  of  the  value  returned  should be alone on a line in
  444. column 1 (do not default to _i_n_t).  If the function does  not
  445. return  a  value  then it should not be given a return type.
  446. _________________________
  447.  
  448. 19.  A common practice in both Bell and local code is to use
  449.      a  space  rather  than  a  tab  after  the  *.  This is
  450.      acceptable.
  451.  
  452. 20.  Discussion of  non-trivial  design  decisions  is  also
  453.      appropriate,  but avoid duplicating information that is
  454.      present in (and clear from) the code.   It's  too  easy
  455.      for such redundant information to get out of date.
  456.  
  457.  
  458.  
  459.  
  460.                        April 18, 1990
  461.  
  462.  
  463.  
  464.  
  465.  
  466.                            - 7 -
  467.  
  468.  
  469. If the value returned requires a long explanation, it should
  470. be  given  in the prologue;  otherwise it can be on the same
  471. line as the return type, tabbed over.  The function name and
  472. formal  parameters  should  be  alone on a line beginning in
  473. column 1.  Each parameter should be declared (do not default
  474. to _i_n_t), with a comment on a single line.  The opening brace
  475. of the function body should also be alone on a  line  begin-
  476. ning  in  column 1.  The function name, argument declaration
  477. list, and opening brace  should  be  separated  by  a  blank
  478. line21.  All local declarations and code within the function
  479. body should be tabbed over at least one tab.
  480.  
  481.      If the function  uses  any  external  variables,  these
  482. should  have  their  own  declarations  in the function body
  483. using the _e_x_t_e_r_n keyword.  If the external  variable  is  an
  484. array  the  array  bounds  must  be  repeated  in the _e_x_t_e_r_n
  485. declaration.  There should also be _e_x_t_e_r_n  declarations  for
  486. all  functions called by a given function.  This is particu-
  487. larly beneficial to  someone  picking  up  code  written  by
  488. another.   If  a function returns a value of type other than
  489. _i_n_t, it is required by the compiler that such  functions  be
  490. declared  before  they are used.  Having the _e_x_t_e_r_n delcara-
  491. tion in the calling function's declarations  section  avoids
  492. all such problems22.
  493.  
  494.      In general each variable declaration  should  be  on  a
  495. separate  line  with a comment describing the role played by
  496. the variable in the function.  If the variable  is  external
  497. or a parameter of type pointer which is changed by the func-
  498. tion, that should be noted in the comment.   All  such  com-
  499. ments for parameters and local variables should be tabbed so
  500. that they line up underneath each other.   The  declarations
  501. should  be  separated  from  the  function's statements by a
  502. blank line.
  503.  
  504.      A local variable should not  be  redeclared  in  nested
  505. blocks23.  Even  though  this  is  valid  C,  the  potential
  506. _________________________
  507.  
  508. 21.  Neither Bell nor local code  has  ever  included  these
  509.      separating  blank  lines, and it is not clear that they
  510.      add anything useful.  Leave them out.
  511.  
  512. 22.  These  rules  tend  to  produce a lot of clutter.  Both
  513.      Bell  and  local  practice  frequently   omits   _e_x_t_e_r_n
  514.      declarations  for _s_t_a_t_i_c variables and functions.  This
  515.      is permitted.  Omission of  declarations  for  standard
  516.      library  routines is also permissible, although if they
  517.      _a_r_e declared it is better to declare  them  within  the
  518.      functions that use them rather than globally.
  519.  
  520. 23.  In  fact,  avoid  any  local declarations that override
  521.      declarations at higher levels.
  522.  
  523.  
  524.  
  525.  
  526.                        April 18, 1990
  527.  
  528.  
  529.  
  530.  
  531.  
  532.                            - 8 -
  533.  
  534.  
  535. confusion is enough that _l_i_n_t will complain  about  it  when
  536. given the ----hhhh option.
  537.  
  538. _6._1.  _E_x_a_m_p_l_e_s
  539.  
  540.  
  541. /*
  542.  *      skyblue()
  543.  *
  544.  *      Determine if the sky is blue.
  545.  */
  546.  
  547. int                     /* TRUE or FALSE */
  548. skyblue()
  549.  
  550. {
  551.         extern int hour;
  552.  
  553.         if (hour < MORNING || hour > EVENING)
  554.                 return(FALSE);  /* black */
  555.         else
  556.                 return(TRUE);   /* blue */
  557. }
  558.  
  559.  
  560. /*
  561.  *      tail(nodep)
  562.  *
  563.  *      Find the last element in the linked list
  564.  *      pointed to by nodep and return a pointer to it.
  565.  */
  566.  
  567. NODE *                  /* pointer to tail of list */
  568. tail(nodep)
  569.  
  570. NODE *nodep;            /* pointer to head of list */
  571.  
  572. {
  573.         register NODE *np;      /* current pointer advances to NULL */
  574.         register NODE *lp;      /* last pointer follows np */
  575.  
  576.         np = lp = nodep;
  577.         while ((np = np->next) != NULL)
  578.                 lp = np;
  579.         return(lp);
  580. }
  581.  
  582.  
  583. _7.  _C_o_m_p_o_u_n_d _S_t_a_t_e_m_e_n_t_s
  584.  
  585.      Compound statements are statements that  contain  lists
  586. of  statements enclosed in braces.  The enclosed list should
  587. be tabbed over one more than the tab position  of  the  com-
  588. pound statement itself.  The opening left brace should be at
  589.  
  590.  
  591.  
  592.                        April 18, 1990
  593.  
  594.  
  595.  
  596.  
  597.  
  598.                            - 9 -
  599.  
  600.  
  601. the end of the line beginning the compound statement and the
  602. closing  right brace should be alone on a line, tabbed under
  603. the beginning of the compound statement.  Note that the left
  604. brace  beginning a function body is the only occurrence of a
  605. left brace which is alone on a line.
  606.  
  607. _7._1.  _E_x_a_m_p_l_e_s
  608.  
  609.  
  610.         if (expr) {
  611.                 statement;
  612.                 statement;
  613.         }
  614.  
  615.         if (expr) {
  616.                 statement;
  617.                 statement;
  618.         } else {
  619.                 statement;
  620.                 statement;
  621.         }
  622.  
  623.      Note that the right brace before the _e_l_s_e and the right
  624. brace  before  the _w_h_i_l_e of a _d_o-_w_h_i_l_e statement (below) are
  625. the only places where a right braces  appears  that  is  not
  626. alone on a line.
  627.  
  628.         for (i = 0; i < MAX; i++) {
  629.                 statement;
  630.                 statement;
  631.         }
  632.  
  633.         while (expr) {
  634.                 statement;
  635.                 statement;
  636.         }
  637.  
  638.         do {
  639.                 statement;
  640.                 statement;
  641.         } while (expr);
  642.  
  643.         switch (expr) {
  644.         case ABC:
  645.         case DEF:
  646.                 statement;
  647.                 break;
  648.         case XYZ:
  649.                 statement;
  650.                 break;
  651.         default:
  652.                 statement;
  653.                 break24;
  654.         }
  655.  
  656.  
  657.  
  658.                        April 18, 1990
  659.  
  660.  
  661.  
  662.  
  663.  
  664.                            - 10 -
  665.  
  666.  
  667. Note that when multiple  _c_a_s_e  labels  are  used,  they  are
  668. placed on separate lines.  The fall through feature of the C
  669. _s_w_i_t_c_h statement should rarely if ever be used when code  is
  670. executed before falling through to the next one.  If this is
  671. done it must be commented for future maintenance.
  672.  
  673.         if (strcmp(reply, "yes") == EQUAL) {
  674.                 statements for yes
  675.                 ...
  676.         } else if (strcmp(reply, "no") == EQUAL) {
  677.                 statements for no
  678.                 ...
  679.         } else if (strcmp(reply, "maybe") == EQUAL) {
  680.                 statements for maybe
  681.                 ...
  682.         } else {
  683.                 statements for none of the above
  684.                 ...
  685.         }
  686.  
  687. The last example is a generalized _s_w_i_t_c_h statement  and  the
  688. tabbing  reflects  the switch between exactly one of several
  689. alternatives rather than a nesting of statements.
  690.  
  691. _8.  _E_x_p_r_e_s_s_i_o_n_s
  692.  
  693. _8._1.  _O_p_e_r_a_t_o_r_s
  694.  
  695.      The old versions of equal-ops =+, =-, =*,  etc.  should
  696. not  be  used.   The  preferred use is +=, -=, *=, etc.  All
  697. binary operators except . and -> should  be  separated  from
  698. their operands by blanks25.  In addition, keywords that  are
  699. followed  by  expressions in parentheses should be separated
  700. from the left parenthesis by a blank26.  Blanks should  also
  701. appear  after  commas in argument lists to help separate the
  702. arguments visually.  On the other hand,  macros  with  argu-
  703. ments and function calls should not have a blank between the
  704. name and the left parenthesis.  In particular, the C prepro-
  705. cessor requires the left parenthesis to be immediately after
  706. _________________________
  707.  
  708. 24.  This _b_r_e_a_k is, strictly speaking, unnecessary,  but  it
  709.      is  required  nonetheless  because  it prevents a fall-
  710.      through error if another _c_a_s_e is added later after  the
  711.      last one.
  712.  
  713. 25.  Some  judgement  is  called  for in the case of complex
  714.      expressions, which may  be  clearer  if  the  ``inner''
  715.      operators   are   not  surrounded  by  spaces  and  the
  716.      ``outer'' ones are.
  717.  
  718. 26.  _S_i_z_e_o_f is an exception, see the discussion of  function
  719.      calls.  Less logically, so is _r_e_t_u_r_n.
  720.  
  721.  
  722.  
  723.  
  724.                        April 18, 1990
  725.  
  726.  
  727.  
  728.  
  729.  
  730.                            - 11 -
  731.  
  732.  
  733. the macro name or else the argument list will not be  recog-
  734. nized.   Unary  operators should not be separated from their
  735. single operand.  Since  C  has  some  unexpected  precedence
  736. rules,  all  expressions involving mixed operators should be
  737. fully parenthesized.
  738.  
  739.      _E_x_a_m_p_l_e_s
  740.  
  741.         a += c + d;
  742.         a = (a + b) / (c * d);
  743.         strp->field = str.fl - ((x & MASK) >> DISP);
  744.         while (*d++ = *s++)
  745.                 ;       /* EMPTY BODY */
  746.  
  747.  
  748. _8._2.  _N_a_m_i_n_g _C_o_n_v_e_n_t_i_o_n_s
  749.  
  750.      Individual projects will no doubt have their own naming
  751. conventions.  There are some general rules however.
  752.  
  753. +o    An initial underscore should not be used for any  user-
  754.      created names27.  UNIX uses it for names that the  user
  755.      should   not  have  to  know  (like  the  standard  I/O
  756.      library)28.
  757.  
  758. +o    Macro names, _t_y_p_e_d_e_f names, and _d_e_f_i_n_e names should  be
  759.      all in CAPS.
  760.  
  761. +o    Variable names, structure tag names, and function names
  762.      should be  in  lower  case29.   Some  macros  (such  as
  763.      _g_e_t_c_h_a_r  and  _p_u_t_c_h_a_r) are in lower case since they may
  764.      also exist as functions.  Care is  needed  when  inter-
  765.      changing  macros  and  functions  since  functions pass
  766.      their parameters by value  whereas  macros  pass  their
  767.      arguments by name substitution30.
  768. _________________________
  769.  
  770. 27.  Trailing underscores should be avoided too.
  771.  
  772. 28.  This convention is reserved for  system  purposes.   If
  773.      you  must have your own private identifiers, begin them
  774.      with a capital letter identifying the package to  which
  775.      they belong.
  776.  
  777. 29.  It  is  best  to  avoid names that differ only in case,
  778.      like _f_o_o and  _F_O_O.   The  potential  for  confusion  is
  779.      considerable.
  780.  
  781. 30.  This  difference also means that carefree use of macros
  782.      requires care when they  are  defined.   Remember  that
  783.      complex  expressions  can  be  used  as parameters, and
  784.      operator-precedence  problems  can  arise  unless   all
  785.      occurrences   of  parameters  in  the  definition  have
  786.      parentheses around them.  There is little that  can  be
  787.  
  788.  
  789.  
  790.                        April 18, 1990
  791.  
  792.  
  793.  
  794.  
  795.  
  796.                            - 12 -
  797.  
  798.  
  799. _8._3.  _C_o_n_s_t_a_n_t_s
  800.  
  801.      Numerical constants should  not  be  coded  directly31.
  802. The  _d_e_f_i_n_e  feature of the C preprocessor should be used to
  803. assign a meaningful name.  This will also make it easier  to
  804. administer  large  programs  since the constant value can be
  805. changed uniformly by changing only the _d_e_f_i_n_e.  The enumera-
  806. tion  data  type  is  the preferred way to handle situations
  807. where a variable takes on only a  discrete  set  of  values,
  808. since additional type checking is available through _l_i_n_t.
  809.  
  810.      There are some cases where the constants 0  and  1  may
  811. appear  as themselves instead of as defines.  For example if
  812. a _f_o_r loop indexes through an array, then
  813.  
  814.         for (i = 0; i < ARYBOUND; i++)
  815.  
  816. is reasonable while the code
  817.  
  818.         fptr = fopen(filename, "r");
  819.         if (fptr == 0)
  820.                 error("can't open %s\n", filename);
  821.  
  822. is not.  In the last example the defined  constant  _N_U_L_L  is
  823. available  as part of the standard I/O library's header file
  824. _s_t_d_i_o._h and must be used in place of the 0.
  825.  
  826. _9.  _P_o_r_t_a_b_i_l_i_t_y
  827.  
  828.      The advantages of portable code are well  known.   This
  829. section  gives  some  guidelines  for writing portable code,
  830. where the definition of portable is taken  to  mean  that  a
  831. source file contains portable code if it can be compiled and
  832. executed on different machines with the only  source  change
  833. being the inclusion of possibly different header files.  The
  834. header files will contain defines and typedefs that may vary
  835. from  machine  to  machine.   Reference  [1] contains useful
  836. information on both style  and  portability.   Many  of  the
  837. recommendations  in  this  document  originated in [1].  The
  838. following is a list of pitfalls to be avoided and  recommen-
  839. dations to be considered when designing portable code:
  840.  
  841. +o    First,  one  must  recognize  that  some   things   are
  842.      inherently  non-portable.   Examples  are  code to deal
  843.      with particular hardware registers such as the  program
  844. _________________________
  845.      done  about  the  problems  caused  by  side effects in
  846.      parameters except to avoid side effects in  expressions
  847.      (a good idea anyway).
  848.  
  849. 31.  At  the  very  least,  any   directly-coded   numerical
  850.      constant  must have a comment explaining the derivation
  851.      of the value.
  852.  
  853.  
  854.  
  855.  
  856.                        April 18, 1990
  857.  
  858.  
  859.  
  860.  
  861.  
  862.                            - 13 -
  863.  
  864.  
  865.      status word, and code that is  designed  to  support  a
  866.      particular  piece  of  hardware such as an assembler or
  867.      I/O driver.  Even in these cases there  are  many  rou-
  868.      tines  and  data organizations that can be made machine
  869.      independent.  It  is  suggested  that  source  file  be
  870.      organized  so that the machine-independent code and the
  871.      machine-dependent code are in separate files.  Then  if
  872.      the  program  is  to be moved to a new machine, it is a
  873.      much  easier  task  to  determine  what  needs  to   be
  874.      changed32.  It  is  also  possible  that  code  in  the
  875.      machine-independent  files  may have uses in other pro-
  876.      grams as well.
  877.  
  878. +o    Pay attention to word sizes.  The following sizes apply
  879.      to  basic types in C for the machines that will be used
  880.      most at IH33:
  881.  
  882.                     type    pdp11   3B   IBM
  883.                     ________________________
  884.                     char        8    8     8
  885.                     short      16   16    16
  886.                     int        16   32    32
  887.                     long       32   32    32
  888.  
  889.      In general if the word size is important, _s_h_o_r_t or _l_o_n_g
  890.      should  be used to get 16 or 32 bit items on any of the
  891.      above machines34.  If a simple loop  counter  is  being
  892.      used  where either 16 or 32 bits will do, then use _i_n_t,
  893.      since it will get the most efficient (natural) unit for
  894.      the current machine35.
  895. _________________________
  896.  
  897. 32.  If you  #_i_f_d_e_f  dependencies,  make  sure  that  if  no
  898.      machine is specified, the result is a syntax error, _n_o_t
  899.      a default machine!
  900.  
  901. 33.  The 3B is a Bell Labs machine.  The VAX, not  shown  in
  902.      the table, is similar to the 3B in these respects.  The
  903.      68000 resembles either the pdp11 or the  3B,  depending
  904.      on the particular compiler.
  905.  
  906. 34.  Any unsigned type other than plain _u_n_s_i_g_n_e_d _i_n_t  should
  907.      be  _t_y_p_e_d_e_fed,  as  such  types  are  highly  compiler-
  908.      dependent.  This is also true of long and  short  types
  909.      other  than  _l_o_n_g  _i_n_t  and  _s_h_o_r_t _i_n_t.  Large programs
  910.      should  have  a  central  header  file  which  supplies
  911.      _t_y_p_e_d_e_fs  for  commonly-used  width-sensitive types, to
  912.      make it easier to change them and  to  aid  in  finding
  913.      width-sensitive code.
  914.  
  915. 35.  Beware   of   making  assumptions  about  the  size  of
  916.      pointers.  They are not always the same  size  as  _i_n_t.
  917.      Nor  are  all  pointers always the same size, or freely
  918.      interconvertible.  Pointer-to-character is a particular
  919.  
  920.  
  921.  
  922.                        April 18, 1990
  923.  
  924.  
  925.  
  926.  
  927.  
  928.                            - 14 -
  929.  
  930.  
  931. +o    Word size also affects shifts and masks.  The code
  932.  
  933.              x &= 0177770
  934.  
  935.      will clear only the three rightmost bits of an _i_n_t on a
  936.      PDP11.   On  a  3B  it will also clear the entire upper
  937.      halfword.  Use
  938.  
  939.              x &= ~07
  940.  
  941.      instead which works properly on all machines36.
  942.  
  943. +o    Code that  takes  advantage  of  the  two's  complement
  944.      representation  of  numbers on most machines should not
  945.      be used.  Optimizations that replace arithmetic  opera-
  946.      tions  with equivalent shifting operations are particu-
  947.      larly suspect.  You should weigh the time savings  with
  948.      the  potential for obscure and difficult bugs when your
  949.      code is moved, say, from a 3B to a 1A.
  950.  
  951. +o    Watch out for signed characters.  On the PDP-11,  char-
  952.      acters  are  sign  extended  when  used in expressions,
  953.      which is not the case on any other machine.  In partic-
  954.      ular,  _g_e_t_c_h_a_r is an integer-valued function (or macro)
  955.      since the value of _E_O_F for the standard I/O library  is
  956.      -1,  which is not possible for a character on the 3B or
  957.      IBM37.
  958.  
  959. +o    The PDP-11 is unique among processors on which C exists
  960.      in  that  the  bytes  are  numbered  from right to left
  961.      within a word.  All other machines (3B, IBM,  Interdata
  962.      8/32, Honeywell) number the bytes from left to right38.
  963.      Hence  any code that depends on the left-right orienta-
  964.      tion of bits in a word deserves special scrutiny.   Bit
  965.      fields  within  structure members will only be portable
  966. _________________________
  967.      trouble  spot  on  machines which do not address to the
  968.      byte.
  969.  
  970. 36.  The or operator ( | ) does not have these problems, nor
  971.      do   bitfields  (which,  unfortunately,  are  not  very
  972.      portable due to defective compilers).
  973.  
  974. 37.  Actually, this is not quite the real reason why _g_e_t_c_h_a_r
  975.      returns  _i_n_t,  but  the  comment  is valid:  code which
  976.      assumes either that characters are signed or that  they
  977.      are  unsigned  is unportable.  It is best to completely
  978.      avoid using _c_h_a_r  to  hold  numbers.   Manipulation  of
  979.      characters  as  if  they  were  numbers  is  also often
  980.      unportable.
  981.  
  982. 38.  Actually,  there  are  some more right-to-left machines
  983.      now, but the comments still apply.
  984.  
  985.  
  986.  
  987.  
  988.                        April 18, 1990
  989.  
  990.  
  991.  
  992.  
  993.  
  994.                            - 15 -
  995.  
  996.  
  997.      so long as two separate fields are  never  concatenated
  998.      and treated as a unit39.  [1,3]
  999.  
  1000. +o    Do not default the boolean test for non-zero, i.e.
  1001.  
  1002.              if (f() != FAIL)
  1003.  
  1004.      is better than
  1005.  
  1006.              if (f())
  1007.  
  1008.      even though _F_A_I_L may have the value  0  which  is  con-
  1009.      sidered  to  mean false by C40.  This will help you out
  1010.      later when  somebody  decides  that  a  failure  return
  1011.      should be -1 instead of 0 41.
  1012.  
  1013. +o    Be suspicious of numeric values appearing in the  code.
  1014.      Even  simple  values  like  0  or  1  could  be  better
  1015.      expressed using defines like _F_A_L_S_E and _T_R_U_E (see previ-
  1016.      ous item)42.  Any other constants appearing in  a  pro-
  1017.      gram  would  be better expressed as a defined constant.
  1018.      This makes it easier to change and also easier to read.
  1019.  
  1020. +o    Become familiar with  existing  library  functions  and
  1021. _________________________
  1022.  
  1023. 39.  The same applies to variables  in  general.   Alignment
  1024.      considerations  and  loader  peculiarities make it very
  1025.      rash  to   assume   that   two   consecutively-declared
  1026.      variables are together in memory, or that a variable of
  1027.      one type is aligned appropriately to be used as another
  1028.      type.
  1029.  
  1030. 40.  A  particularly  notorious case is using _s_t_r_c_m_p to test
  1031.      for string equality, where the result should _n_e_v_e_r _e_v_e_r
  1032.      be  defaulted.   The  preferred approach is to define a
  1033.      macro _S_T_R_E_Q:
  1034.  
  1035.              #define STREQ(a, b) (strcmp((a), (b)) == 0)
  1036.  
  1037.  
  1038. 41.  An exception is commonly made for predicates, which are
  1039.      functions which meet the following restrictions:
  1040.  
  1041. +o    Has no other purpose than to return true or false.
  1042.  
  1043. +o    Returns 0 for false, 1 for true, nothing else.
  1044.  
  1045. +o    Is named so that the meaning of (say) a  `true'  return
  1046.      is  absolutely  obvious.   Call  a predicate _i_s_v_a_l_i_d or
  1047.      _v_a_l_i_d, not _c_h_e_c_k_v_a_l_i_d.
  1048.  
  1049. 42.  Actually, _Y_E_S and _N_O often read better.
  1050.  
  1051.  
  1052.  
  1053.  
  1054.                        April 18, 1990
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.                            - 16 -
  1061.  
  1062.  
  1063.      defines43.  You should not be writing your  own  string
  1064.      compare  routine, or making your own defines for system
  1065.      structures44.  Not only does this waste your time,  but
  1066.      it  prevents  your program from taking advantage of any
  1067.      microcode assists or other means of  improving  perfor-
  1068.      mance of system routines45.
  1069.  
  1070. +o    Use _l_i_n_t.  It is a valuable tool for  finding  machine-
  1071.      dependent  constructs  as well as other inconsistencies
  1072.      or program bugs that pass the compiler46.
  1073.  
  1074. _1_0.  _L_i_n_t
  1075.  
  1076.      _L_i_n_t is a C program checker [2] that examines C  source
  1077. files  to  detect  and report type incompatibilities, incon-
  1078. sistencies between function definitions and calls, potential
  1079. program  bugs,  etc.   It  is  expected  that  projects will
  1080. require programs to use _l_i_n_t as part of the official  accep-
  1081. tance procedure47.  In addition, work is going on in depart-
  1082. ment 5521 to modify _l_i_n_t so that it will check for adherence
  1083. to the standards in this document.
  1084.  
  1085.      It is still too early  to  say  exactly  which  of  the
  1086. _________________________
  1087.  
  1088. 43.  But not _t_o_o familiar.  The internal details of  library
  1089.      facilities,  as  opposed  to their external interfaces,
  1090.      are subject to change without warning.  They  are  also
  1091.      often quite unportable.
  1092.  
  1093. 44.  Or,  especially,  writing  your  own  code  to  control
  1094.      terminals.  Use the _t_e_r_m_c_a_p package.
  1095.  
  1096. 45.  It  also  makes  your  code  less readable, because the
  1097.      reader has to figure out whether you're doing something
  1098.      special  in  that  reimplemented  stuff  to justify its
  1099.      existence.  Furthermore,  it's  a  fruitful  source  of
  1100.      bugs.
  1101.  
  1102. 46.  The  use  of  _l_i_n_t  on   all   programs   is   strongly
  1103.      recommended.   It  is difficult to eliminate complaints
  1104.      about functions whose return value is not used (in  the
  1105.      current  version  of  C,  at  least),  but  most  other
  1106.      messages from _l_i_n_t really do indicate something  wrong.
  1107.      The  -h, -p, -a, -x, and -c options are worth learning.
  1108.      All of them will complain about some legitimate things,
  1109.      but  they will also pick up many botches.  Note that -p
  1110.      checks function-call type-consistency for only a subset
  1111.      of  Unix library routines, so programs should be linted
  1112.      both  with   and   without   this   option   for   best
  1113.      ``coverage''.
  1114.  
  1115. 47.  Yes.
  1116.  
  1117.  
  1118.  
  1119.  
  1120.                        April 18, 1990
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.                            - 17 -
  1127.  
  1128.  
  1129. standards given here will be checked by _l_i_n_t.  In some cases
  1130. such  as  whether a comment is misleading or incorrect there
  1131. is little hope of mechanical checking.  In other cases  such
  1132. as  checking  that  the  opening brace of a function body is
  1133. alone on a line in column  1,  the  test  has  already  been
  1134. added48.  Future bulletins will  be  used  to  announce  new
  1135. additions to _l_i_n_t as they occur.
  1136.  
  1137.      It should be noted that the best way to use _l_i_n_t is not
  1138. as  a  barrier  that must be overcome before official accep-
  1139. tance of a program, but rather as a  tool  to  use  whenever
  1140. major changes or additions to the code have been made.  _L_i_n_t
  1141. can find obscure bugs and insure portability before problems
  1142. occur.
  1143.  
  1144. _1_1.  _S_p_e_c_i_a_l _C_o_n_s_i_d_e_r_a_t_i_o_n_s
  1145.  
  1146.      This  section  contains  some  miscellaneous  do's  and
  1147. don'ts.
  1148.  
  1149. +o    Don't change syntax via macro substitution.   It  makes
  1150.      the program unintelligible to all but the perpetrator.
  1151.  
  1152. +o    There is a time and a  place  for  embedded  assignment
  1153.      statements49.  In some constructs there  is  no  better
  1154.      way  to  accomplish the results without making the code
  1155.      bulkier and less readable.  The _w_h_i_l_e loop  in  section
  1156.      8.1 is one example of an appropriate place.  Another is
  1157.      the common code segment:
  1158.  
  1159.              while ((c = getchar()) != EOF) {
  1160.                      process the character
  1161.              }
  1162.  
  1163.      Using embedded assignment statements  to  improve  run-
  1164.      time performance is also possible.  However, one should
  1165.      consider  the  tradeoff  between  increased  speed  and
  1166.      decreased  maintainability  that  results when embedded
  1167.      assignments are used in artificial places.   For  exam-
  1168.      ple, the code:
  1169.  
  1170.              a = b + c;
  1171.              d = a + r;
  1172.  
  1173.      should not be replaced by
  1174.  
  1175. _________________________
  1176.  
  1177. 48.  Little of this is relevant at U of T.  The  version  of
  1178.      _l_i_n_t that we have lacks these mods.
  1179.  
  1180. 49.  The ++++++++ and -------- operators count as assignment statements.
  1181.      So, for many purposes, do functions with side effects.
  1182.  
  1183.  
  1184.  
  1185.  
  1186.                        April 18, 1990
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.                            - 18 -
  1193.  
  1194.  
  1195.  
  1196.              d = (a = b + c) + r;
  1197.  
  1198.      even though the latter may save one cycle.   Note  that
  1199.      in  the  long  run  the time difference between the two
  1200.      will decrease as the optimizer  gains  maturity,  while
  1201.      the  difference in ease of maintenance will increase as
  1202.      the human memory of what's going on in the latter piece
  1203.      of code begins to fade50.
  1204.  
  1205. +o    There is also a time and  place  for  the  ternary  ? :
  1206.      operator  and  the  binary comma operator.  The logical
  1207.      expression   operand   before   the   ? :   should   be
  1208.      parenthesized:
  1209.  
  1210.              (x >= 0) ? x : -x
  1211.  
  1212.      Nested ? : operators can be  confusing  and  should  be
  1213.      avoided  if  possible.   There  are  some  macros  like
  1214.      _g_e_t_c_h_a_r where they can be useful.  The  comma  operator
  1215.      can  also be useful in _f_o_r statements to provide multi-
  1216.      ple initializations or incrementations.
  1217.  
  1218. +o    Goto statements should be  used  sparingly  as  in  any
  1219.      well-structured code51.  The main place where they  can
  1220.      be  usefully employed is to break out of several levels
  1221.      of _s_w_i_t_c_h, _f_o_r, and _w_h_i_l_e nesting52, e.g.
  1222.  
  1223.              for (...)
  1224.                      for (...) {
  1225.                              ...
  1226.                              if (disaster)
  1227.                                      goto error;
  1228.                      }
  1229.              ...
  1230.      error:
  1231.              clean up the mess
  1232.  
  1233.      When a _g_o_t_o is necessary the accompanying label  should
  1234.      be  alone  on a line and tabbed one tab position to the
  1235. _________________________
  1236.  
  1237. 50.  Note also that  side  effects  within  expressions  can
  1238.      result  in code whose semantics are compiler-dependent,
  1239.      since C's order of evaluation is  explicitly  undefined
  1240.      in most places.  Compilers do differ.
  1241.  
  1242. 51.  The _c_o_n_t_i_n_u_e statement is almost as bad.  _B_r_e_a_k is less
  1243.      troublesome.
  1244.  
  1245. 52.  The need to do such a thing may indicate that the inner
  1246.      constructs   should  be  broken  out  into  a  separate
  1247.      function, with a success/failure return code.
  1248.  
  1249.  
  1250.  
  1251.  
  1252.                        April 18, 1990
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.                            - 19 -
  1259.  
  1260.  
  1261.      left of the associated code that follows.
  1262.  
  1263. +o    This committee recommends that programmers not rely  on
  1264.      automatic   beautifiers   for  the  following  reasons.
  1265.      First, the main person who benefits from  good  program
  1266.      style  is  the  programmer himself.  This is especially
  1267.      true in the early design of handwritten  algorithms  or
  1268.      pseudo-code.  Automatic beautifiers can only be applied
  1269.      to complete, syntactically correct programs  and  hence
  1270.      are  not available when the need for attention to white
  1271.      space and indentation is greatest.   It  is  also  felt
  1272.      that  programmers  can  do a better job of making clear
  1273.      the complete visual layout of a function or file,  with
  1274.      the  normal  attention  to detail of a careful program-
  1275.      mer53.  Sloppy programmers should learn to  be  careful
  1276.      programmers  instead of relying on a beautifier to make
  1277.      their code readable.  Finally, it is  felt  that  since
  1278.      beautifiers  are  non-trivial  programs that must parse
  1279.      the source, the burden of maintaining them in the  face
  1280.      of the continuing evolution of C is not worth the bene-
  1281.      fits gained by such a program.
  1282.  
  1283. _1_2.  _P_r_o_j_e_c_t _D_e_p_e_n_d_e_n_t _S_t_a_n_d_a_r_d_s
  1284.  
  1285.      Individual projects may wish  to  establish  additional
  1286. standards beyond those given here.  The following issues are
  1287. some of those that should be adddressed by each project pro-
  1288. gram administration group.
  1289.  
  1290. +o    What additional naming conventions should be  followed?
  1291.      In  particular, systematic prefix conventions for func-
  1292.      tional grouping of global data and also  for  structure
  1293.      or union member names can be useful.
  1294.  
  1295. +o    What kind of include file organization  is  appropriate
  1296.      for the project's particular data hierarchy?
  1297.  
  1298. +o    What procedures should  be  established  for  reviewing
  1299.      _l_i_n_t  complaints?   A tolerance level needs to be esta-
  1300.      blished in concert with the  _l_i_n_t  options  to  prevent
  1301.      unimportant  complaints  from  hiding  complaints about
  1302.      real bugs or inconsistencies.
  1303.  
  1304. +o    If a project establishes its own archive libraries,  it
  1305.      should plan on supplying a lint library file [2] to the
  1306.      system administrators.  This will allow _l_i_n_t  to  check
  1307.      for compatible use of library functions.
  1308.  
  1309. _________________________
  1310.  
  1311. 53.  In other words, some of the visual layout  is  dictated
  1312.      by  intent rather than syntax.  Beautifiers cannot read
  1313.      minds.
  1314.  
  1315.  
  1316.  
  1317.  
  1318.                        April 18, 1990
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.                            - 20 -
  1325.  
  1326.  
  1327. _1_3.  _C_o_n_c_l_u_s_i_o_n
  1328.  
  1329.      A set of standards has been presented for C programming
  1330. style.   One  of the most important points is the proper use
  1331. of white space and comments so that  the  structure  of  the
  1332. program  is  evident  from  the layout of the code.  Another
  1333. good idea to keep in mind when writing code is  that  it  is
  1334. likely  that  you or someone else will be asked to modify it
  1335. or make it run  on  a  different  machine  sometime  in  the
  1336. future.
  1337.  
  1338.      As with any standard, it must be followed if it  is  to
  1339. be  useful.   The  Indian  Hill version of _l_i_n_t will enforce
  1340. those standards that are amenable to automatic checking.  If
  1341. you have trouble following any of these standards don't just
  1342. ignore them.  Programmers at Indian Hill should bring  their
  1343. problems  to  the  Software  Development  System  Group (Lee
  1344. Kirchhoff, contact) in department 5522.  Programmers outside
  1345. Indian  Hill  should contact the Processor Application Group
  1346. (Layne Cannon, contact) in department 5512 54.
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377. _________________________
  1378.  
  1379. 54.  At U of T Zoology, it's Henry Spencer in 336B.
  1380.  
  1381.  
  1382.  
  1383.  
  1384.                        April 18, 1990
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.                            - 21 -
  1391.  
  1392.  
  1393.                          RRRReeeeffffeeeerrrreeeennnncccceeeessss
  1394.  
  1395.  
  1396.  
  1397. [1]  B.A. Tague, "C Language Portability",  Sept  22,  1977.
  1398.      This  document issued by department 8234 contains three
  1399.      memos by R.C. Haight, A.L. Glasser, and T.L. Lyon deal-
  1400.      ing with style and portability.
  1401.  
  1402. [2]  S.C. Johnson, "Lint, a C  Program  Checker",  Technical
  1403.      Memorandum, 77-1273-14, September 16, 1977.
  1404.  
  1405. [3]  R.W. Mitze, "The 3B/PDP-11 Swabbing Problem",  Memoran-
  1406.      dum for File, 1273-770907.01MF, September 14, 1977.
  1407.  
  1408. [4]  R.A. Elliott and D.C.  Pfeffer,  "3B  Processor  Common
  1409.      Diagnostic  Standards- Version 1", Memorandum for File,
  1410.      5514-780330.01MF, March 30, 1978.
  1411.  
  1412. [5]  R.W. Mitze, "An Overview of C Compilation of UNIX  User
  1413.      Processes  on  the  3B",  Memorandum  for  File,  5521-
  1414.      780329.02MF, March 29, 1978.
  1415.  
  1416. [6]  B.W. Kernighan and  D.M.  Ritchie,  _T_h_e  _C  _P_r_o_g_r_a_m_m_i_n_g
  1417.      _L_a_n_g_u_a_g_e, Prentice-Hall 1978.
  1418.  
  1419.  
  1420.  
  1421.  
  1422.  
  1423.  
  1424.  
  1425.  
  1426.  
  1427.  
  1428.  
  1429.  
  1430.  
  1431.  
  1432.  
  1433.  
  1434.  
  1435.  
  1436.  
  1437.  
  1438.  
  1439.  
  1440.  
  1441.  
  1442.  
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.                        April 18, 1990
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.                            - 22 -
  1457.  
  1458.  
  1459.  
  1460. /*
  1461.  * TTTThhhheeee CCCC SSSSttttyyyylllleeee SSSSuuuummmmmmmmaaaarrrryyyy SSSShhhheeeeeeeetttt                            Block comment,
  1462.  * by Henry Spencer, U of T Zoology                     describes file.
  1463.  */
  1464.  
  1465. #include <errno.h>                                      Headers; don't nest.
  1466.  
  1467. typedef int     SEQNO;  /* ... */                       Global definitions.
  1468. #define STREQ(a, b)     (strcmp((a), (b)) == 0)
  1469.  
  1470. static char *foo = NULL;        /* ... */               Global declarations.
  1471. struct bar {                                            Static whenever poss.
  1472.         SEQNO alpha;    /* ... */
  1473. #               define  NOSEQNO 0
  1474.         int beta;       /* ... */                       Don't assume 16 bits.
  1475. };
  1476.  
  1477. /*
  1478.  * Many unnecessary braces, to show where.              Functions.
  1479.  */
  1480. static int              /* what is returned */          Don't default int.
  1481. bletch(a)
  1482. int a;                  /* ... */                       Don't default int.
  1483. {
  1484.         int bar;                /* ... */
  1485.         extern int errno;       /* ..., changed here */
  1486.         extern char *index();
  1487.  
  1488.         if (foobar() != FAIL) {                 if (!isvalid()) {
  1489.                 return(OK);                             errno = ERANGE;
  1490.         }                                       } else {
  1491.                                                         x = &y + z->field;
  1492.         while (x == (y & MASK)) {               }
  1493.                 f += (x >= 0) ? x : -x;
  1494.         }                                       for (i = 0; i < BOUND; i++) {
  1495.                                                         /* lint -h[p]cax. */
  1496.         do {                                    }
  1497.                 /* Avoid nesting ?: */
  1498.         } while (index(a, b) != NULL);          if (STREQ(x, "foo")) {
  1499.                                                         x |= 07;        /* 07 is... */
  1500.         switch (...) {                          } else if (STREQ(x, "bar")) {
  1501.         case ABC:                                       x &= ~077;      /* 077 is... */
  1502.         case DEF:                               } else if (STREQ(x, "ugh")) {
  1503.                 printf("...", a, b);                    /* Avoid gotos */
  1504.                 break;                          } else {
  1505.         case XYZ:                                       /* and continues. */
  1506.                 x = y;                          }
  1507.                 /* FALLTHROUGH */
  1508.         default:                                while ((c = getc()) != EOF)
  1509.                 /* Limit imbedded =s. */                ;       /* NULLBODY */
  1510.                 break;
  1511.         }
  1512. }
  1513.  
  1514.  
  1515.  
  1516.                        April 18, 1990
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.                            - 23 -
  1523.  
  1524.  
  1525.      ---------------
  1526.  
  1527.  
  1528.  
  1529.  
  1530.  
  1531.  
  1532.  
  1533.  
  1534.  
  1535.  
  1536.  
  1537.  
  1538.  
  1539.  
  1540.  
  1541.  
  1542.  
  1543.  
  1544.  
  1545.  
  1546.  
  1547.  
  1548.  
  1549.  
  1550.  
  1551.  
  1552.  
  1553.  
  1554.  
  1555.  
  1556.  
  1557.  
  1558.  
  1559.  
  1560.  
  1561.  
  1562.  
  1563.  
  1564.  
  1565.  
  1566.  
  1567.  
  1568.  
  1569.  
  1570.  
  1571.  
  1572.  
  1573.  
  1574.  
  1575.  
  1576.  
  1577.  
  1578.  
  1579.  
  1580.  
  1581.  
  1582.                        April 18, 1990
  1583.  
  1584.  
  1585.