home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / software / 5035 < prev    next >
Encoding:
Internet Message Format  |  1992-12-17  |  2.3 KB

  1. From: defaria@hpcuhe.cup.hp.com (Andy DeFaria)
  2. Date: Thu, 17 Dec 1992 00:17:17 GMT
  3. Subject: Re: C Code Layout
  4. Message-ID: <45360002@hpcuhe.cup.hp.com>
  5. Organization: Hewlett Packard, Cupertino
  6. Path: sparky!uunet!cs.utexas.edu!sun-barr!ames!saimiri.primate.wisc.edu!zaphod.mps.ohio-state.edu!usc!sdd.hp.com!col.hp.com!news.dtc.hp.com!hpscit.sc.hp.com!hplextra!hpcss01!hpcuhe!defaria@hpcuhe.cup.hp.com
  7. Newsgroups: comp.software-eng
  8. References: <1992Dec15.023235.17090@seq.uncwil.edu>
  9. Lines: 61
  10.  
  11. >/ hpcuhe:comp.software-eng / phardie@nastar.uucp (Pete Hardie) /  7:38 am  Dec 15, 1992 /
  12.  
  13. >A brief comment (NPI :->) here.  I find that the brace style:
  14. >
  15. >    if(condition)
  16. >    {
  17. >        statement;
  18. >        ...
  19. >    }
  20. >
  21. >is more readable than this style:
  22. >
  23. >    if(condition) {
  24. >        statement;
  25. >        ...
  26. >    }
  27. >
  28. >for one main reason - the alignment of the open and close braces is the same,
  29. >and this (to me) ties the block of code together.  IMHO, this outweighs the
  30. >additional single line per block of vertical space.
  31.  
  32. That's because you view it as an open ({) and corresponding close (}).  To me,
  33. such statements, while they require a close, should not require an open.  In
  34. other words the assumption should be that I always have a compound statement
  35. and must tell the compiler when it's done.  C required that I also specify an
  36. open but I see no reason to devote a full line to it!  I detest:
  37.  
  38.     if (condition)
  39.        statement;
  40.         statement;
  41. or     
  42.     if (condition) statement;
  43.     statement;
  44.  
  45. It's just too easy to misread the fact that C will only execute the first
  46. statement if condition is true.  I would code:
  47.  
  48.     if (condition) {
  49.        statement;
  50.     } /* if */
  51.  
  52.     statement;
  53.  
  54. Note I label my terminating close (}).  Now think about it.  When eyeballing
  55. the above code and reading the if statement: Do you really care where it has
  56. opened or when it has closed?  To me the open is inconsequential: Of course it
  57. starts but where does it end?  Consequently:
  58.  
  59.     if (condition) {
  60.        statement;
  61.     } else {
  62.        statement;
  63.     } /* if */
  64.  
  65. Here you again scan the if statement and ask yourself: "OK if this condition
  66. is true I'll be executing this code here.  OK where does it end?  Hmm.. Oh
  67. here is the else portion and this is executed if the above if is false.  OK
  68. and this whole if terminates at... A ha here's the terminating brace".
  69.  
  70. At least this is how I find myself reading code.
  71.  
  72.