Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Coding style for small if statements Date: Thu, 20 Feb 2020 10:10:59 -0800 Organization: A noiseless patient Spider Lines: 64 Message-ID: <864kvldt8s.fsf@linuxsc.com> References: <20200131234613.59047e037939ceb549b8e6d6@gmail.com> <9951ac5f-a84c-4dde-87cd-c2bcaf5afb89@googlegroups.com> <20200214152137.63b6090cd9fc01e72bfe7da7@g{oogle}mail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="deb22254a47215e7a717fd925781483c"; logging-data="8778"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+XgSM7af0dI/iMsUDQUH+phrRAZUBPHkI=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:kuqaQBlO90SqC6Sd9VL932S51qo= sha1:3AIlylaiF2YZjJENqs0URMDc6ys= Xref: csiph.com comp.lang.c:150092 ram@zedat.fu-berlin.de (Stefan Ram) writes: [...] > There are several different ways to format texts with braces > and parentheses. [...] > > from Kernighan's B tutorial from 1972: > > if(---) > { ----- > ----- } > else if(---) > { ----- > ----- } > else if(----) > { ----- > ----- } This choice of example is misleading. The code outline shown is given in the tutorial as part of explaining how 'else if' can be used to select one choice from among several alternatives, not as an example of layout style. All other code shown in the tutorial uses K&R bracing style, where the braces go on the same lines as the 'if's or 'else's, except of course for the last where it goes on a line by itself. As far as compactness (which was mentioned in the ... text) goes, writing this example using K&R style if(---){ ----- ----- } else if(---){ ----- ----- } else if(----){ ----- ----- } takes just one additional line. Also the closing braces line up vertically with the statements that start the nested block. There is quantitative evidence that lining up group closings (ie, the close braces in C) in this way gives lower error rates than other choices of positioning group closings. In particular, a closing brace style like this { ----- ----- } gives better results, in terms of observed error rates, than a Lisp closing brace style, like this { ----- ----- } (I have deliberately left off a leading if() or while(), etc, to avoid going off an a tangent about where open braces should go.) The results regarding error rates for different closing positions were already known well over forty years ago, so there is no excuse for making any other choice going forward.