For historical reasons, we allow one exception to the above rules: if anif
statement has noelse
orelse if
clauses, then the curly braces for the controlled statement or the line breaks inside the curly braces may be omitted if as a result the entireif
statement appears on either a single line (in which case there is a space between the closing parenthesis and the controlled statement) or on two lines (in which case there is a line break after the closing parenthesis and there are no braces). For example, the following forms are allowed under this exception.
if (x == kFoo) return new Foo();
if (x == kBar)
return new Bar(arg1, arg2, arg3);
if (x == kQuz) { return new Quz(1, 2, 3); }
Use this style only when the statement is brief, and consider that conditional statements with complex conditions or controlled statements may be more readable with curly braces. Some projects require curly braces always.
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAHOzFAfW%2B9xq3noJKb99bGF-Xzr6_W6hTvipJ_1HfPVa8fj%2Bg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAMGbLiH_AvqM2QFGcg2VeDQVhA-xK8NZbiZb%2BbPAvsU2_F5QJg%40mail.gmail.com.
In the past, I've had review comments asking me to remove braces. Would it be reasonable to say that this is explicitly allowed going forward?
--PK
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAHOzFDhkZ2%3DgULEDMXDFAjnZVjKPdeQkmu49%2BXncVTsLuo6og%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAHtyhaQmf6nChwmw51ieJ3e9qZc44Sm4SBr0XbABtCfH8XB5Ww%40mail.gmail.com.
We should have "git cl format" adjust braces to whatever the style guide says. If I'm not mistaken it currently doesn't touch braces because it's hard to get the rules right, but requiring braces around conditionals would be easy to implement. So we should do that. (Plus it would be weird to have tooling prefer the optional format that's allowed but not encouraged.)Whether that means "the style guide says braces are always required" or "the style guide says braces are always allowed; in theory you can choose not to use them for one-liners but in practice git cl format will add them back" is just semantics.
--On Wed, Nov 11, 2020 at 1:36 PM <dan...@chromium.org> wrote:--On Wed, Nov 11, 2020 at 1:29 PM 'Peter Kasting' via cxx <c...@chromium.org> wrote:On Wed, Nov 11, 2020 at 10:27 AM Daniel Cheng <dch...@chromium.org> wrote:In the past, I've had review comments asking me to remove braces. Would it be reasonable to say that this is explicitly allowed going forward?I don't know that such a statement pulls its weight in the Chromium style guide, but I certainly think it would be reasonable if that's the conclusion of this thread to push back and reference this thread.I would like to see reviewers not require braces to be added or removed from CLs that do not violate the style guide.And I do agree that the guide explicitly allows excluding them for 2-line `if+controled statement` situations. This is more restricted than in the past, but it's clear that it allows simple early-outs etc to stay without braces. It also allows them to have braces.--PK
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAHOzFDhkZ2%3DgULEDMXDFAjnZVjKPdeQkmu49%2BXncVTsLuo6og%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAHtyhaQmf6nChwmw51ieJ3e9qZc44Sm4SBr0XbABtCfH8XB5Ww%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAH%3DT95RDYX8XiuRGM5WkFwd95p-Ca%2Btt6TieyRnDSRVrmqO%3DKw%40mail.gmail.com.
We should have "git cl format" adjust braces to whatever the style guide says. If I'm not mistaken it currently doesn't touch braces because it's hard to get the rules right,
PK
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAHtyhaQ5HYPQJBHeZnDuDJVaMg3OwP_DqgkdY9q-Br7k2Vnz7A%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/2a6cbe79-d454-4ca3-890a-a21bf0b6bc91n%40chromium.org.
if (foo)
a();
b();
To summarize a bit where we are, I think we have consensus on the following points:
- The style guide allows omitting braces (in certain cases), and the update doesn't change that.
- We shouldn't care about this if tooling can handle the issue.
- Since the style guide permits both, code reviewers shouldn't mandate one style or another.
I don't think this settles the question, though, as I think these points still leave plenty unresolved:
- The style guide allows omitting braces, but does it recommend it? IMO, the language is unclear on this, and to me, the situation feels a bit how MACRO_CASE used to be allowed for enumerators, but is now banned completely: Too many people are invested in the current way of doing things, but it's not the ideal rule.
- It's been mentioned that clang-tidy can enforce allowing both styles, but (1) clang-tidy doesn't run as part of git cl format, so it has a higher cost to apply (I've often gone back and fixed issues only caught by clang-tidy after I've uploaded a change), and (2) since both styles are allowed, tools still can't make decisions about whether or not to include the braces, only that they're included or omitted correctly. Human judgment is still required in each case.
- Reviewers can still use consistency and subjective readability to bikeshed about whether or not to include braces. (If nobody thought the style without braces wasn't more readable, we wouldn't be discussing this, after all.) A number of people have mentioned having the experience that they've included the braces, and been told to remove them. (It seems rarer to get review comments in the other direction, which makes me think people feel more strongly about omitting them than including them.)
I'd like to make two additional arguments:First, Nico raised how -Wmisleading-indentation and -Wunreachable-code would catch something like this, so we don't need to worry about it:if (foo)
a();
b();I hadn't even considered this, but I realized this is actually a technical argument for preferring always using braces. For one thing, these compile-time checks are not going to catch the common scenario where you might (1) do a search-and-replace, and then (2) run clang-format ("fixing" the "misleading" indentation; oops). (There's also the additional toil of having to go in and add the braces where they weren't before. If the changes were temporary, you then have to remove them again...)Second, while experienced developers already used to omitting the braces may feel more comfortable with that style, I think it's more important to think about how new developers are going to feel about it. (There will always be more future developers than current developers, after all, at least on a healthy project.) Having a single consistent rule here (either "always omit whenever possible," or "always include") is going to be easier to learn than something that requires experience and judgment. (And I would argue "always include" is the easier rule to follow, but I realize that's subjective.)Finally, can someone state the arguments in favor of omitting braces? I feel like most of the arguments so far have been, "If the style guide allows it, why not allow it?" I think the case would be stronger with more affirmative arguments, though, such as, "If this were banned, my code would suffer because reason X." I'm assuming it's based on finding the more compact style more readable (early return in guards seems like a frequent example), just like we still have an 80 column limit, but I'm just guessing what the arguments are here. :-)
Finally, can someone state the arguments in favor of omitting braces? I feel like most of the arguments so far have been, "If the style guide allows it, why not allow it?" I think the case would be stronger with more affirmative arguments, though, such as, "If this were banned, my code would suffer because reason X." I'm assuming it's based on finding the more compact style more readable (early return in guards seems like a frequent example), just like we still have an 80 column limit, but I'm just guessing what the arguments are here. :-)
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CACwGi-711weLx%2BHJdZKJuARV3OsXpXyTGK5B31ZmhyiH4FD95w%40mail.gmail.com.
To summarize a bit where we are, I think we have consensus on the following points:
- The style guide allows omitting braces (in certain cases), and the update doesn't change that.
- We shouldn't care about this if tooling can handle the issue.
- Since the style guide permits both, code reviewers shouldn't mandate one style or another.
I don't think this settles the question, though, as I think these points still leave plenty unresolved:
- The style guide allows omitting braces, but does it recommend it? IMO, the language is unclear on this, and to me, the situation feels a bit how MACRO_CASE used to be allowed for enumerators, but is now banned completely: Too many people are invested in the current way of doing things, but it's not the ideal rule.
- It's been mentioned that clang-tidy can enforce allowing both styles, but (1) clang-tidy doesn't run as part of git cl format, so it has a higher cost to apply (I've often gone back and fixed issues only caught by clang-tidy after I've uploaded a change), and (2) since both styles are allowed, tools still can't make decisions about whether or not to include the braces, only that they're included or omitted correctly. Human judgment is still required in each case.
- Reviewers can still use consistency and subjective readability to bikeshed about whether or not to include braces. (If nobody thought the style without braces wasn't more readable, we wouldn't be discussing this, after all.) A number of people have mentioned having the experience that they've included the braces, and been told to remove them. (It seems rarer to get review comments in the other direction, which makes me think people feel more strongly about omitting them than including them.)
I'd like to make two additional arguments:First, Nico raised how -Wmisleading-indentation and -Wunreachable-code would catch something like this, so we don't need to worry about it:if (foo)
a();
b();I hadn't even considered this, but I realized this is actually a technical argument for preferring always using braces. For one thing, these compile-time checks are not going to catch the common scenario where you might (1) do a search-and-replace, and then (2) run clang-format ("fixing" the "misleading" indentation; oops). (There's also the additional toil of having to go in and add the braces where they weren't before. If the changes were temporary, you then have to remove them again...)Second, while experienced developers already used to omitting the braces may feel more comfortable with that style, I think it's more important to think about how new developers are going to feel about it. (There will always be more future developers than current developers, after all, at least on a healthy project.) Having a single consistent rule here (either "always omit whenever possible," or "always include") is going to be easier to learn than something that requires experience and judgment. (And I would argue "always include" is the easier rule to follow, but I realize that's subjective.)Finally, can someone state the arguments in favor of omitting braces? I feel like most of the arguments so far have been, "If the style guide allows it, why not allow it?" I think the case would be stronger with more affirmative arguments, though, such as, "If this were banned, my code would suffer because reason X." I'm assuming it's based on finding the more compact style more readable (early return in guards seems like a frequent example), just like we still have an 80 column limit, but I'm just guessing what the arguments are here. :-)
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CACwGi-711weLx%2BHJdZKJuARV3OsXpXyTGK5B31ZmhyiH4FD95w%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/d7211e63-b1b6-4f0f-86cb-8c1c28f05425n%40chromium.org.
I think this decision is a bit different than other possible deviations from the style guide, in that the style guide explicitly states that projects can make a choice. "Some projects require curly braces always," is an extremely unusual statement to see in the style guide, which generally doesn't suggest project-specific carve-outs. It also suggests opting out of the historical rule, but not vice versa.That said, unless there's a sudden outpouring of support to the contrary, I don't think there's consensus for picking a single style on this, so let's consider that question decided.I think there's still two more decisions to make in this thread:
- What should tooling do?
- How should reviewers handle disagreements about whether or not to omit braces?
With regards to (1), it sounds like we'd like to configure clang-tidy to (mostly) enforce the style guide rule, which SGTM.
With regards to (2), overall opinion seems to be leaning towards making this almost a "no comment" issue (whichever style-compliant choice the author makes should be respected), but I believe there have been at least one or two comments in this thread about preferring to omit in certain cases, which I think (unfairly?) privileges the historical option over the non-historical option.--On Fri, Nov 13, 2020 at 7:06 AM Nico Weber <tha...@chromium.org> wrote:Like Peter said, any argument here that's not chromium-specific in some way violates the "we're trying to follow the google style guide" default.On Fri, Nov 13, 2020 at 10:02 AM Igor Bukanov <ig...@vivaldi.com> wrote:--On Thursday, November 12, 2020 at 6:10:13 PM UTC+1 Vladimir Levin wrote:> I think verbosity would be my main argument here. I don't particularly mind braces, but saying>> if (!client)> return;>> is clear enough for meThis argument suggest to treat "if (condition) control_transfer;" differently from the "if" followed by non-jumpy code. Perhaps the rule should be that for the control transfer case there should be no braces unless "if (condition)" or the transfer like "return long_expression" does not fit one line. Or at least to always require braces only for non-jumpy code but allow not to use braces for jumps.
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/d7211e63-b1b6-4f0f-86cb-8c1c28f05425n%40chromium.org.
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CACwGi-5MGApKZTTWrfcp3bJ-StZCG7%3DetoUTLtB0RfqePWvrwQ%40mail.gmail.com.
With regards to (2), overall opinion seems to be leaning towards making this almost a "no comment" issue (whichever style-compliant choice the author makes should be respected), but I believe there have been at least one or two comments in this thread about preferring to omit in certain cases, which I think (unfairly?) privileges the historical option over the non-historical option.
On Fri, Nov 13, 2020 at 1:41 PM K. Moon <km...@chromium.org> wrote:I think this decision is a bit different than other possible deviations from the style guide, in that the style guide explicitly states that projects can make a choice. "Some projects require curly braces always," is an extremely unusual statement to see in the style guide, which generally doesn't suggest project-specific carve-outs. It also suggests opting out of the historical rule, but not vice versa.That said, unless there's a sudden outpouring of support to the contrary, I don't think there's consensus for picking a single style on this, so let's consider that question decided.I think there's still two more decisions to make in this thread:
- What should tooling do?
If someone has bandwidth to try and implement something in clang-format, then they should implement something in clang-format :) IIRC adding this was harder than it at first seemed though.
- How should reviewers handle disagreements about whether or not to omit braces?
With regards to (1), it sounds like we'd like to configure clang-tidy to (mostly) enforce the style guide rule, which SGTM.It's imho not a great use of time for reviewers to argue about this. The goal of code review is to make code better, and reviewers should try to ask themselves if they're adding value worth the back-and-forth overhead.Personally I'd even say it's not worth it to make clang-tidy complain about it; if we add too many chatty diags to clang-tidy people will learn to ignore tricium comments. But I'm willing to have my mind changed on this point. https://chromium.googlesource.com/chromium/src/+/master/docs/clang_tidy.md#Adding-a-new-check describes the process for adding checks to clang-tidy, if you want to give it a try.
--With regards to (2), overall opinion seems to be leaning towards making this almost a "no comment" issue (whichever style-compliant choice the author makes should be respected), but I believe there have been at least one or two comments in this thread about preferring to omit in certain cases, which I think (unfairly?) privileges the historical option over the non-historical option.--On Fri, Nov 13, 2020 at 7:06 AM Nico Weber <tha...@chromium.org> wrote:Like Peter said, any argument here that's not chromium-specific in some way violates the "we're trying to follow the google style guide" default.On Fri, Nov 13, 2020 at 10:02 AM Igor Bukanov <ig...@vivaldi.com> wrote:--On Thursday, November 12, 2020 at 6:10:13 PM UTC+1 Vladimir Levin wrote:> I think verbosity would be my main argument here. I don't particularly mind braces, but saying>> if (!client)> return;>> is clear enough for meThis argument suggest to treat "if (condition) control_transfer;" differently from the "if" followed by non-jumpy code. Perhaps the rule should be that for the control transfer case there should be no braces unless "if (condition)" or the transfer like "return long_expression" does not fit one line. Or at least to always require braces only for non-jumpy code but allow not to use braces for jumps.
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/d7211e63-b1b6-4f0f-86cb-8c1c28f05425n%40chromium.org.
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CACwGi-5MGApKZTTWrfcp3bJ-StZCG7%3DetoUTLtB0RfqePWvrwQ%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAMGbLiGBZ30V-HEe9Z_AnpcRYJxxvW%3D2hEZYgbBcOEVtM1kL1Q%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CACwGi-7m-XNTWtdSzmbX4phYynzCScU4az3xbvV-KNBVS5uzew%40mail.gmail.com.