My 2 pennies is braces add unnecessary clutter and impair readability when used on a *single-line* statement. I count comments, that are on their own line as statement(s). For example: BAD: if (cond) // Comment foo(); GOOD: if (cond) { // Comment foo(); } BAD: if (cond) { foo(); // Comment } GOOD: if (cond) foo(); // Comment BAD: if (cond) for(;;) foo() GOOD: if (cond) { for(;;) foo() } Some new-ish languages like Go and Swift went to always require braces. However, I've never seen a study, which concluded that always requiring braces has an overall positive effect on code quality. Is there such a thing? Lacking that, it becomes a matter of personal taste and preference and anecdotal evidence in favour of one or the other style. Speaking of which, as I constantly run clang-format on the blocks of code, that I currently modify, I don't think I've ever misplaced a statement. ~chill -- Compiler scrub, Arm -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200616/44ad6328/attachment.html>
Hi, FWIW, I'm on the always-add-braces camp, for the already mentioned reasons and also the following: * It makes it easier to add and remove one-off debug code that you don't intend to commit * It makes it easier for my editor to jump over the whole block while scrolling back and forth around code I also like chill's suggestion, since it at least solves the second issue. Cheers, Diana On Tue, 16 Jun 2020 at 10:35, Momchil Velikov via llvm-dev <llvm-dev at lists.llvm.org> wrote:> > My 2 pennies is braces add unnecessary clutter and impair readability when > used on a *single-line* statement. I count comments, that are on their > own line as statement(s). > > For example: > > BAD: > > if (cond) > // Comment > foo(); > > > GOOD: > if (cond) { > // Comment > foo(); > } > > BAD: > if (cond) { > foo(); // Comment > } > > GOOD: > if (cond) > foo(); // Comment > > BAD: > if (cond) > for(;;) > foo() > > > GOOD: > if (cond) { > for(;;) > foo() > } > > Some new-ish languages like Go and Swift went to always require braces. However, I've never > seen a study, which concluded that always requiring braces has an overall positive effect > on code quality. > > Is there such a thing? > > Lacking that, it becomes a matter of personal taste and preference and anecdotal evidence > in favour of one or the other style. Speaking of which, as I constantly run clang-format > on the blocks of code, that I currently modify, I don't think I've ever misplaced a statement. > > ~chill > > -- > Compiler scrub, Arm > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
On Tue, Jun 16, 2020 at 10:35 AM Momchil Velikov via llvm-dev <llvm-dev at lists.llvm.org> wrote:> My 2 pennies is braces add unnecessary clutter and impair readability when > used on a *single-line* statement. I count comments, that are on their > own line as statement(s).+1 for this. I think braces around single-line statements can be allowed, but they really shouldn't be mandated, and that's been my personal policy for reviews. In particular, if (!is_transform_applicable) { return {}; } is very aggravating clutter. Braces should be required around multi-line statements. Note: BAD: for (...) for (...) single_line_statement; GOOD: for (...) { for (...) single_line_statement; } Cheers, Nicolai -- Lerne, wie die Welt wirklich ist, aber vergiss niemals, wie sie sein sollte.
Did this conversation reach a conclusion? My ad hoc tally says that a slight majority of the responders preferred to fully brace statements and no one wanted to totally eliminate braces. The technical arguments for fully braced statements were 1) it's considered a slightly safer coding style and 2) commit diffs with fully braced statements may be slightly more to the point. I didn't register any technical arguments for less-than-fully-braced statement -- the preference seemed to be aesthetic. I may have missed a technical argument. Certainly an "always use braces" rule would be simpler than what's documented now in the LLVM Coding Standards [1]. Another option would be to make braces a developer's choice, and ask that those omitting braces please follow the rules documented in [1]. [1] https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements On 6/18/20, 3:56 AM, "llvm-dev on behalf of Nicolai Hähnle via llvm-dev" <llvm-dev-bounces at lists.llvm.org on behalf of llvm-dev at lists.llvm.org> wrote: External email: Use caution opening links or attachments On Tue, Jun 16, 2020 at 10:35 AM Momchil Velikov via llvm-dev <llvm-dev at lists.llvm.org> wrote: > My 2 pennies is braces add unnecessary clutter and impair readability when > used on a *single-line* statement. I count comments, that are on their > own line as statement(s). +1 for this. I think braces around single-line statements can be allowed, but they really shouldn't be mandated, and that's been my personal policy for reviews. In particular, if (!is_transform_applicable) { return {}; } is very aggravating clutter. Braces should be required around multi-line statements. Note: BAD: for (...) for (...) single_line_statement; GOOD: for (...) { for (...) single_line_statement; } Cheers, Nicolai -- Lerne, wie die Welt wirklich ist, aber vergiss niemals, wie sie sein sollte. _______________________________________________ LLVM Developers mailing list llvm-dev at lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev