home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / Documents / Others / C-Commandments.txt < prev    next >
Text File  |  1993-01-12  |  16KB  |  364 lines

  1.  
  2.      The Ten Commandments for C Programmers 
  3.                (Annotated Edition)
  4.  
  5.                 Henry Spencer
  6.  
  7.  
  8. 1    Thou shalt run lint frequently and study its pronounce-
  9.      ments  with  care, for verily its perception and judge-
  10.      ment oft exceed thine.
  11.                This   is   still   wise   counsel,
  12.                although   many   modern  compilers
  13.                search out many of the  same  sins,
  14.                and  there  are often problems with
  15.                lint being aged and infirm, or una-
  16.                vailable  in  strange lands.  There
  17.                are other tools, such  as  Saber C,
  18.                useful to similar ends.
  19.                ``Frequently'' means thou  shouldst
  20.                draw  thy  daily  guidance from it,
  21.                rather than hoping  thy  code  will
  22.                achieve lint's blessing by a sudden
  23.                act  of  repentance  at  the   last
  24.                minute.  De-linting a program which
  25.                has never  been  linted  before  is
  26.                often  a  cleaning  of  the stables
  27.                such as thou wouldst  not  wish  on
  28.                thy  worst  enemies.  Some observe,
  29.                also,  that  careful  heed  to  the
  30.                words  of lint can be quite helpful
  31.                in debugging.
  32.                ``Study'' doth  not  mean  mindless
  33.                zeal  to  eradicate  every  byte of
  34.                lint output-if for no other reason,
  35.                because thou just canst not shut it
  36.                up about some things-but that  thou
  37.                should know the cause of its unhap-
  38.                piness and understand  what  worri-
  39.                some sign it tries to speak of.
  40.  
  41.  
  42. 2    Thou shalt not follow the NULL pointer, for  chaos  and
  43.      madness await thee at its end.
  44.                Clearly the  holy  scriptures  were
  45.                mis-transcribed  here, as the words
  46.                should have been ``null  pointer'',
  47.                to  minimize  confusion between the
  48.                concept of null  pointers  and  the
  49.                macro  NULL  (of  which more anon).
  50.                Otherwise, the meaning is plain.  A
  51.                null   pointer  points  to  regions
  52.                filled with dragons,  demons,  core
  53.                dumps,  and  numberless  other foul
  54.                creatures, all of which delight  in
  55.                frolicing  in  thy  program if thou
  56.                disturb  their   sleep.    A   null
  57.                pointer  doth  not  point to a 0 of
  58.                any type, despite some  blasphemous
  59.                old  code  which  impiously assumes
  60.                this.
  61.  
  62.  
  63. 3    Thou shalt cast all function arguments to the  expected
  64.      type  if  they  are not of that type already, even when
  65.      thou art convinced that this is unnecessary, lest  they
  66.      take  cruel  vengeance upon thee when thou least expect
  67.      it.
  68.                A programmer should understand  the
  69.                type  structure  of  his  language,
  70.                lest great misfortune befall him.
  71.                Contrary to the  heresies  espoused
  72.                by  some  of  the  dwellers  on the
  73.                Western Shore, `int' and `long' are
  74.                not  the  same type.  The moment of
  75.                their  equivalence  in   size   and
  76.                representation  is  short,  and the
  77.                agony  that  awaits  believers   in
  78.  
  79.  
  80.  
  81.                      November 25, 1992
  82.  
  83.  
  84.  
  85.  
  86.  
  87.                            - 2 -
  88.  
  89.  
  90.                their interchangeability shall last
  91.                forever  and   ever   once   64-bit
  92.                machines become common.
  93.                Also, contrary to the beliefs  com-
  94.                mon among the more backward inhabi-
  95.                tants  of  the   Polluted   Eastern
  96.                Marshes,  `NULL'  does  not  have a
  97.                pointer type, and must be  cast  to
  98.                the  correct  type  whenever  it is
  99.                used as a function argument.
  100.                (The words  of  the  prophet  Ansi,
  101.                which  permit NULL to be defined as
  102.                having the type `void *',  are  oft
  103.                taken  out of context and misunder-
  104.                stood.  The prophet was granting  a
  105.                special  dispensation  for  use  in
  106.                cases of  great  hardship  in  wild
  107.                lands.  Verily, a righteous program
  108.                must make its own way  through  the
  109.                Thicket  Of  Types  without  lazily
  110.                relying  on  this  rarely-available
  111.                dispensation to solve all its prob-
  112.                lems.   In  any  event,  the  great
  113.                deity Dmr who created C hath wisely
  114.                endowed  it  with  many  types   of
  115.                pointers, not just one, and thus it
  116.                would still be necessary to convert
  117.                the  prophet's  NULL to the desired
  118.                type.)
  119.                It may be thought that the  radical
  120.                new   blessing   of  ``prototypes''
  121.                might eliminate the need  for  cau-
  122.                tion about argument types.  Not so,
  123.                brethren.  Firstly, when confronted
  124.                with  the  twisted  strangeness  of
  125.                variable numbers of arguments,  the
  126.                problem  returns...  and he who has
  127.                not  kept  his  faith   strong   by
  128.                repeated practice shall surely fall
  129.                to this subtle trap.  Secondly, the
  130.                wise  men  have observed that reli-
  131.                ance on prototypes doth  open  many
  132.                doors  to  strange errors, and some
  133.                indeed had  hoped  that  prototypes
  134.                would  be  decreed  for purposes of
  135.                error checking but would not  cause
  136.                implicit    conversions.    Lastly,
  137.                reliance  on   prototypes   causeth
  138.                great  difficulty in the Real World
  139.                today, when many cling to  the  old
  140.                ways  and  the old compilers out of
  141.                desire or  necessity,  and  no  man
  142.                knoweth  what  machine his code may
  143.                be asked to run on tomorrow.
  144.  
  145.  
  146. 4    If thy header files fail to declare the return types of
  147.      thy  library functions, thou shalt declare them thyself
  148.      with the  most  meticulous  care,  lest  grievous  harm
  149.      befall thy program.
  150.                The prophet Ansi,  in  her  wisdom,
  151.                hath  added that thou shouldst also
  152.                scourge thy Suppliers,  and  demand
  153.                on  pain  of  excommunication  that
  154.                they  produce  header  files   that
  155.                declare  their  library  functions.
  156.                For truly, only they know the  pre-
  157.                cise   form   of   the  incantation
  158.                appropriate to invoking their magic
  159.                in the optimal way.
  160.                The  prophet  hath  also  commented
  161.                that  it  is  unwise, and leads one
  162.                into the pits of damnation and sub-
  163.                tle  bugs,  to  attempt  to declare
  164.                such  functions  thyself  when  thy
  165.                header files do the job right.
  166.  
  167.  
  168. 5    Thou shalt  check  the  array  bounds  of  all  strings
  169.      (indeed,  all  arrays),  for  surely  where thou typest
  170.      ``foo'' someone someday shall type ``supercalifragilis-
  171.      ticexpialidocious''.
  172.                As demonstrated by the deeds of the
  173.                Great  Worm,  a consequence of this
  174.                commandment is that robust  produc-
  175.                tion software should never make use
  176.                of gets(), for it is truly  a  tool
  177.                of   the   Devil.   Thy  interfaces
  178.                should always inform  thy  servants
  179.                of  the  bounds  of thy arrays, and
  180.                servants who spurn such  advice  or
  181.                quietly fail to follow it should be
  182.                dispatched forthwith to the Land Of
  183.                Rm,  where  they  can do no further
  184.                harm to thee.
  185.  
  186.  
  187. 6    If a function be advertised to return an error code  in
  188.      the  event  of  difficulties, thou shalt check for that
  189.  
  190.  
  191.                      November 25, 1992
  192.  
  193.  
  194.  
  195.  
  196.  
  197.                            - 3 -
  198.  
  199.  
  200.      code, yea, even though the checks triple  the  size  of
  201.      thy  code  and produce aches in thy typing fingers, for
  202.      if thou thinkest ``it cannot happen to me'',  the  gods
  203.      shall surely punish thee for thy arrogance.
  204.                All true believers doth wish for  a
  205.                better   error-handling  mechanism,
  206.                for explicit checks of return codes
  207.                are tiresome in the extreme and the
  208.                temptation to omit them  is  great.
  209.                But   until   the  far-off  day  of
  210.                deliverance cometh, one  must  walk
  211.                the  long  and  winding  road  with
  212.                patience and care, for thy  Vendor,
  213.                thy   Machine,   and  thy  Software
  214.                delight  in  surprises  and   think
  215.                nothing  of  producing subtly mean-
  216.                ingless results on the  day  before
  217.                thy Thesis Oral or thy Big Pitch To
  218.                The Client.
  219.                Occasionally, as with the  ferror()
  220.                feature of stdio, it is possible to
  221.                defer error checking until the  end
  222.                when  a  cumulative  result  can be
  223.                tested, and  this  often  produceth
  224.                code  which is shorter and clearer.
  225.                Also, even the most  zealous  beli-
  226.                ever should exercise some judgement
  227.                when dealing with  functions  whose
  228.                failure is totally uninteresting...
  229.                but beware, for the cast to void is
  230.                a  two-edged  sword  that  sheddeth
  231.                thine own blood without remorse.
  232.  
  233. 7    Thou shalt study thy libraries and strive  not  to  re-
  234.      invent  them  without cause, that thy code may be short
  235.      and readable and thy days pleasant and productive.
  236.                Numberless are the unwashed heathen
  237.                who  scorn their libraries on vari-
  238.                ous  silly  and  spurious  grounds,
  239.                such as blind worship of the Little
  240.                Tin  God  (also  known  as  ``Effi-
  241.                ciency'').   While  it is true that
  242.                some features of  the  C  libraries
  243.                were  ill-advised,  by and large it
  244.                is better and cheaper  to  use  the
  245.                works  of others than to persist in
  246.                re-inventing the square wheel.  But
  247.                thou  should  take  the greatest of
  248.                care   to   understand   what   thy
  249.                libraries promise, and what they do
  250.                not, lest thou rely  on  facilities
  251.                that may vanish from under thy feet
  252.                in future.
  253.  
  254. 8    Thou shalt make thy  program's  purpose  and  structure
  255.      clear  to  thy  fellow  man by using the One True Brace
  256.      Style, even if thou likest it not, for  thy  creativity
  257.      is  better  used  in  solving problems than in creating
  258.      beautiful new impediments to understanding.
  259.                These words, alas, have caused some
  260.                uncertainty  among  the novices and
  261.                the converts, who knoweth  not  the
  262.                ancient   wisdoms.   The  One  True
  263.                Brace Style  referred  to  is  that
  264.                demonstrated in the writings of the
  265.                First   Prophets,   Kernighan   and
  266.                Ritchie.   Often  and  again  it is
  267.                criticized by the ignorant as  hard
  268.                to  use, when in truth it is merely
  269.                somewhat difficult  to  learn,  and
  270.                thereafter is wonderfully clear and
  271.                obvious, if perhaps a bit sensitive
  272.                to mistakes.
  273.                While thou might think  that  thine
  274.                own  ideas  of  brace style lead to
  275.                clearer  programs,  thy  successors
  276.                will  not  thank  thee  for it, but
  277.                rather shall revile thy  works  and
  278.                curse  thy  name,  and word of this
  279.                might get  to  thy  next  employer.
  280.                Many  customs  in this life persist
  281.                because they ease friction and pro-
  282.                mote  productivity  as  a result of
  283.                universal  agreement,  and  whether
  284.                they   are  precisely  the  optimal
  285.                choices is much less important.  So
  286.                it is with brace style.
  287.                As a lamentable side  issue,  there
  288.                has   been  some  unrest  from  the
  289.                fanatics  of  the  Pronoun  Gestapo
  290.                over the use of the word ``man'' in
  291.                this Commandment, for they  believe
  292.                that  great efforts and loud shout-
  293.                ing   devoted   to    the    ritual
  294.  
  295.  
  296.  
  297.                      November 25, 1992
  298.  
  299.  
  300.  
  301.  
  302.  
  303.                            - 4 -
  304.  
  305.  
  306.                purification  of  the language will
  307.                somehow redound to the  benefit  of
  308.                the  downtrodden  (whose  real  and
  309.                grievous woes tendeth to  get  lost
  310.                amidst  all that thunder and fury).
  311.                When preaching the  gospel  to  the
  312.                narrow of mind and short of temper,
  313.                the word ``creature'' may  be  sub-
  314.                stituted as a suitable pseudoBibli-
  315.                cal term free of the taint of  Pol-
  316.                itical Incorrectness.
  317.  
  318. 9    Thy external identifiers shall be unique in  the  first
  319.      six characters, though this harsh discipline be irksome
  320.      and the years of  its  necessity  stretch  before  thee
  321.      seemingly  without end, lest thou tear thy hair out and
  322.      go mad on that fateful day when thou desirest  to  make
  323.      thy program run on an old system.
  324.                Though some hasty zealots cry ``not
  325.                so; the Millenium is come, and this
  326.                saying is obsolete  and  no  longer
  327.                need  be  supported'', verily there
  328.                be many, many  ancient  systems  in
  329.                the  world, and it is the decree of
  330.                the dreaded  god  Murphy  that  thy
  331.                next  employment  just  might be on
  332.                one.  While thou sleepest, he plot-
  333.                teth  against thee.  Awake and take
  334.                care.
  335.                It is, note carefully,  not  neces-
  336.                sary  that  thy identifiers be lim-
  337.                ited to a length of six characters.
  338.                The  only requirement that the holy
  339.                words place upon thee is uniqueness
  340.                within  the  first six.  This often
  341.                is not so hard  as  the  belittlers
  342.                claimeth.
  343.  
  344. 10   Thou shalt foreswear, renounce,  and  abjure  the  vile
  345.      heresy  which  claimeth that ``All the world's a VAX'',
  346.      and have no commerce with the  benighted  heathens  who
  347.      cling  to  this  barbarous belief, that the days of thy
  348.      program may be long even though the days of thy current
  349.      machine be short.
  350.                This particular heresy bids fair to
  351.                be  replaced by ``All the world's a
  352.                Sun'' or ``All the world's a  386''
  353.                (this  latter  being a particularly
  354.                revolting invention of Satan),  but
  355.                the words apply to all such without
  356.                limitation.  Beware, in particular,
  357.                of  the  subtle  and terrible ``All
  358.                the  world's  a  32-bit  machine'',
  359.                which  is  almost  true  today  but
  360.                shall cease to  be  so  before  thy
  361.                resume grows too much longer.
  362.  
  363.                      November 25, 1992
  364.