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: Sat, 04 Apr 2020 14:09:48 -0700 Organization: A noiseless patient Spider Lines: 77 Message-ID: <86blo7nejn.fsf@linuxsc.com> References: <20200131234613.59047e037939ceb549b8e6d6@gmail.com> <86blpyfvv6.fsf@linuxsc.com> <86k14db27f.fsf@linuxsc.com> <86v9mm7ghq.fsf@linuxsc.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="962b2addb8ebbeafdd69920a30d34173"; logging-data="27074"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19n0+zVGiVrCSX6VM1IzqiCvKgrGusMFtc=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:D7HOQuJiSHqP1O+WwLlciUksc/0= sha1:BNnQIqATTW5S6G2TUhYe/faUqAY= Xref: csiph.com comp.lang.c:151022 Ike Naar writes: > On 2020-03-30, Tim Rentsch wrote: > >> I don't understand the gain of putting open braces on lines by >> themselves. I've asked lots of people who choose to write code in >> that style what they think the benefits are, but the answers I've >> gotten always seem circular in one way or another. "I like it >> because it's more readable" sounds suspiciously like "I like it >> because I like it." My asking the question is not meant to be >> argumentative. I ask because I want to understand other people's >> views. So far though as best I can make out the reason is they >> like it because that's what they are used to. Can you shed any >> more light on this question? > > I can give you a reason why I find it more readable: > the vertical alignment of corresponding opening and closing braces > makes it easier to see that they form a pair. I sometimes write controlled statements (of if(), for(), etc), on the same line as the if(), etc. When I do this I do not use braces unless they are necessary for some other reason. When I write a controlled statement on a subsequent line, even if it is only a single (non-block) statement, I always use braces. Do you always use braces for controlled statements not on the same line as the controlling phrase (if(), etc)? If you do, doesn't the if() or for(), etc, vertically aligned with the closing brace, stand out to show the range/pairing of each group? I don't see how putting in an additional line with just an open brace adds any useful information. (Please note that I am not saying there isn't any, just that I don't see how there is.) > Question to you, as a follower of K&R style: > You don't see much gain in putting the opening brace on a line by > itself. What is the benefit of putting the closing brace on a > line by itself? First I assume the alternative you want me to consider for this question is not this if(...){ something; } next statement... but this if(...){ something; } (with the understanding that horizontal white space is not part of the question). Incidentally this latter form is sometimes called "Lisp style". Some C programmers do use Lisp-style placement of closing braces (even though I myself do not). Surely everyone knows this, but for completeness, K&R style is this if(...){ something; } with the closing brace lined up under the 'i' of 'if' or the 'f' of 'for', etc. I think K&R-style closing brace placement is a better choice than, eg, Lisp-style placement, because studies have found that defect rates are lowest for the placement of closing braces where K&R style puts them. (I don't know of any corresponding study for the placement of open braces.) This result matches my personal experience, although of course that experience is evaluated subjectively rather than trying to do some sort of objective measurement.