home *** CD-ROM | disk | FTP | other *** search
- From: defaria@hpcuhe.cup.hp.com (Andy DeFaria)
- Date: Thu, 17 Dec 1992 00:17:17 GMT
- Subject: Re: C Code Layout
- Message-ID: <45360002@hpcuhe.cup.hp.com>
- Organization: Hewlett Packard, Cupertino
- 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
- Newsgroups: comp.software-eng
- References: <1992Dec15.023235.17090@seq.uncwil.edu>
- Lines: 61
-
- >/ hpcuhe:comp.software-eng / phardie@nastar.uucp (Pete Hardie) / 7:38 am Dec 15, 1992 /
-
- >A brief comment (NPI :->) here. I find that the brace style:
- >
- > if(condition)
- > {
- > statement;
- > ...
- > }
- >
- >is more readable than this style:
- >
- > if(condition) {
- > statement;
- > ...
- > }
- >
- >for one main reason - the alignment of the open and close braces is the same,
- >and this (to me) ties the block of code together. IMHO, this outweighs the
- >additional single line per block of vertical space.
-
- That's because you view it as an open ({) and corresponding close (}). To me,
- such statements, while they require a close, should not require an open. In
- other words the assumption should be that I always have a compound statement
- and must tell the compiler when it's done. C required that I also specify an
- open but I see no reason to devote a full line to it! I detest:
-
- if (condition)
- statement;
- statement;
- or
- if (condition) statement;
- statement;
-
- It's just too easy to misread the fact that C will only execute the first
- statement if condition is true. I would code:
-
- if (condition) {
- statement;
- } /* if */
-
- statement;
-
- Note I label my terminating close (}). Now think about it. When eyeballing
- the above code and reading the if statement: Do you really care where it has
- opened or when it has closed? To me the open is inconsequential: Of course it
- starts but where does it end? Consequently:
-
- if (condition) {
- statement;
- } else {
- statement;
- } /* if */
-
- Here you again scan the if statement and ask yourself: "OK if this condition
- is true I'll be executing this code here. OK where does it end? Hmm.. Oh
- here is the else portion and this is executed if the above if is false. OK
- and this whole if terminates at... A ha here's the terminating brace".
-
- At least this is how I find myself reading code.
-
-