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: Sun, 23 Feb 2020 10:14:44 -0800 Organization: A noiseless patient Spider Lines: 99 Message-ID: <86k14db27f.fsf@linuxsc.com> References: <20200131234613.59047e037939ceb549b8e6d6@gmail.com> <86blpyfvv6.fsf@linuxsc.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="5b1dd2a70f059f247db25722f2582bc8"; logging-data="19931"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18eA2iCPECMVsSdRKW4cHKED1BqE3I980c=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:6U6EIjzbSH3KiOrRneJks49k1f0= sha1:E1xSuB3UUZ0QuMyUnJbgjQfwzEk= Xref: csiph.com comp.lang.c:150189 Ike Naar writes: > On 2020-02-16, Tim Rentsch wrote: > >> scott@slp53.sl.home (Scott Lurndal) writes: >> >>> Braces on a line by themselves just waste vertical space. >> >> Typically about 9 or 10 percent (comparing Allman style to >> K&R style). A significant cost. > > Curiously, one often sees > > if (condition) { > > do_this(); > do_that(); > } else /* ... */ > > which has no brace on a line by itself, Up to the time of reading your posting, I don't remember ever seeing that. So of course I went looking. Here are some statistics. Looking at C source (.c files), there were roughly 84,500 files, with about 88.5 million lines of source, in files having at least one control statement (if, for, while, do, or else) with an open brace at the end of the line. The average length is, naturally, just under 1,050 lines. (The files used were taken from a conveniently available set of mostly open source code, not selected for any particular source or reason.) Of these files, the vast majority had no occurrences of a blank line at the start of a nested block. Looking at just the subset of files that have at least one instance of a blank line at the start of a nested block, there were 6640 files holding roughly 22.8 million lines of source. This gives an average length of just over 3,433 lines (more than three times as long as the average of the larger set). Again looking at just the subset of files that have at least one instance of a blank line at the start of a nested block, there were 15385 nested blocks with a blank line at the start, out of 1097658 possibles, or just over 1.4%. However, the subset has a lot more than its share of control statements with nested blocks, so even counting the files that have no blank lines at the start of nested block, nested blocks that start with a blank line make up almost 0.83% of the total. > but wastes the same amount of vertical space as > > if (condition) > { > do_this(); > do_that(); > } else /* ... */ There are a few problems with this reasoning. The first is that blank lines at the start of nested (K&R-style) blocks are rare, statistically speaking, so the space wasted is not the same in the two cases. The average percentage of blank- line-started nested blocks is just over 21%. That means overall lengthenings of 2.1% versus 10%. The second is that people who prefer braces on separate lines usually don't write if/else in the layout shown above, they write this: if (condition) { do_this(); do_that(); } else /* ... */ Also there is the question of consistency. Are you suggesting that only those blocks that would have a blank line at the start be written with brace on a line by itself? Or that all nested blocks be written with brace on a line by itself? The first would be inconsistent. The second would be longer than the existing code that (sometimes) uses blank lines at the start of nested blocks. There is also another consideration. In code that /sometimes/ starts nested blocks with a blank line, the choice to put in a blank line conveys extra information. If nested blocks always write brace on a line by itself, the mostly empty brace lines don't convey that extra information. > I personally find the latter more readable because the opening > and closing braces are properly aligned. How much of that is just because it's what you are more used to? I understand there is a non-trivial hysteresis. However, if you run a trial period of, say, three months, during which time you work exclusively with K&R-style bracing, what effect does that have on your sense of the relative readability of the two styles?