s***@chromium.org
2018-11-16 01:11:10 UTC
Sorry for reviving this, but there's still an entry in our webUI styleguide
regarding if-statement braces, and people are still confused about whether
things need braces.
According to google styleguide:
if (...) {
...one line;
} else {
...one line;
}
if (...) {
......... one line;
}
will all need braces. the only case that would not need braces is if the
if-statement AND the one-liner both fit on the same line:
if (...) one liner;
It seems like the discussion in this thread had converged on "lets remove
chrome's styleguide on if-statements, and just follow google's styleguide"
but this hasn't happened yet. If there's no further concerns I'll be making
a CL to update our styleguide and also add an eslint rule to enforce this
in the future.
regarding if-statement braces, and people are still confused about whether
things need braces.
According to google styleguide:
if (...) {
...one line;
} else {
...one line;
}
if (...) {
......... one line;
}
will all need braces. the only case that would not need braces is if the
if-statement AND the one-liner both fit on the same line:
if (...) one liner;
It seems like the discussion in this thread had converged on "lets remove
chrome's styleguide on if-statements, and just follow google's styleguide"
but this hasn't happened yet. If there's no further concerns I'll be making
a CL to update our styleguide and also add an eslint rule to enforce this
in the future.
https://groups.google.com/a/chromium.org/d/msg/chromium-dev/eIEY-0UKQxw/O_wiacC9CAAJ
tl;dr: In C++ code, braces are optional for single-statement then clauses.
Some feel they should be mandatory. Nevertheless, as a code reviewer, don't
force people to add/remove them just because it conflicts with your own
personal preference.
I believe there used to be specific guidance in the Chromium C++ style
guide (that *kinda* applied to JS) about preferring less curlies for
simple conditionals, but I don't see it any more. Maybe I was imagining it
or it has disappeared (and maybe is still in effect from a different style
guide, i.e. Google?). Either way, I don't see a strong case for asking
folks to NOT use curlies where they like them (other than maybe precedence).
I've always thought the "omit curlies" rule was a bit unsafe (see: goto
fail; bug). I followed "omit curlies" in reviews/my code because it's What
I Was Told. Now that the Google JS style guide has a clear opinion on this
(and most of the folks that hated curlies have moved on ;)), I think it'd
be fine to adopt your proposal above.
It'd obviously be nice to update existing, non-compliant code if possible.
Also note: clang-format -style=Chromium currently overrides Google's
https://github.com/llvm-mirror/clang/blob/master/lib/Format/Format.cpp#L714
-- Dan
Thanks,
--
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups
"Chromium-dev" group.
To view this discussion on the web visit
https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CANNW_Qp%3D0iVS7PEVMr_ZvgMRL_UnfdZx-wRN-ZKo7QJmJJ_XAA%40mail.gmail.com
<https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CANNW_Qp%3D0iVS7PEVMr_ZvgMRL_UnfdZx-wRN-ZKo7QJmJJ_XAA%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
tl;dr: In C++ code, braces are optional for single-statement then clauses.
Some feel they should be mandatory. Nevertheless, as a code reviewer, don't
force people to add/remove them just because it conflicts with your own
personal preference.
Based on the fact that there are quite a few occurrences of
if (foo)
bar();
for example here
<https://cs.chromium.org/chromium/src/chrome/browser/resources/settings/prefs/prefs.js?l=274-275>,
and
if (foo)
bar();
else
baz();
for example here
<https://cs.chromium.org/chromium/src/chrome/browser/resources/settings/controls/settings_slider.js?type=cs&q=else+file:%5Esrc/chrome/browser/resources/settings/+package:%5Echromium$&l=68-71>,
in WebUI JS code, as well as stylistic comments I've received in past code
reviews, I believe that a single-line if statement is interpreted as "*single-line
|if| block without counting the |if| line, and single line |else/else if|
block without counting the |else/else if| line*".
This is my interpretation as well.if (foo)
bar();
for example here
<https://cs.chromium.org/chromium/src/chrome/browser/resources/settings/prefs/prefs.js?l=274-275>,
and
if (foo)
bar();
else
baz();
for example here
<https://cs.chromium.org/chromium/src/chrome/browser/resources/settings/controls/settings_slider.js?type=cs&q=else+file:%5Esrc/chrome/browser/resources/settings/+package:%5Echromium$&l=68-71>,
in WebUI JS code, as well as stylistic comments I've received in past code
reviews, I believe that a single-line if statement is interpreted as "*single-line
|if| block without counting the |if| line, and single line |else/else if|
block without counting the |else/else if| line*".
I believe there used to be specific guidance in the Chromium C++ style
guide (that *kinda* applied to JS) about preferring less curlies for
simple conditionals, but I don't see it any more. Maybe I was imagining it
or it has disappeared (and maybe is still in effect from a different style
guide, i.e. Google?). Either way, I don't see a strong case for asking
folks to NOT use curlies where they like them (other than maybe precedence).
My preference would be to remove the Chromium styleguide variation
(based on this interpretation), and adhere to the Google styleguide
<https://google.github.io/styleguide/jsguide.html#formatting-braces>.
In other words the snippets above would be invalid, and the valid versions
would be
if (foo) bar();
if (foo) {
bar();
}
if (foo) {
bar();
} else {
baz();
}
^ This seems fine to me.(based on this interpretation), and adhere to the Google styleguide
<https://google.github.io/styleguide/jsguide.html#formatting-braces>.
In other words the snippets above would be invalid, and the valid versions
would be
if (foo) bar();
if (foo) {
bar();
}
if (foo) {
bar();
} else {
baz();
}
I've always thought the "omit curlies" rule was a bit unsafe (see: goto
fail; bug). I followed "omit curlies" in reviews/my code because it's What
I Was Told. Now that the Google JS style guide has a clear opinion on this
(and most of the folks that hated curlies have moved on ;)), I think it'd
be fine to adopt your proposal above.
It'd obviously be nice to update existing, non-compliant code if possible.
Also note: clang-format -style=Chromium currently overrides Google's
https://github.com/llvm-mirror/clang/blob/master/lib/Format/Format.cpp#L714
-- Dan
Thanks,
Demetrios
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google
Groups "Chromium-dev" group.
To view this discussion on the web visit
https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/1e400180-07f6-4034-90bd-5cd59f719fe3%40chromium.org
<https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/1e400180-07f6-4034-90bd-5cd59f719fe3%40chromium.org?utm_medium=email&utm_source=footer>
.
--Depending on how "single-line if statement" is interpreted from the
- Omit curly braces for single-line if statements
may result in a change in the styling. So this may be a "redundant"
no-op change to remove this style guidance (and fall through to the Google
style guide) or it may be a change.
The debate is a bit involved. Please ask if you'd like details.
--- Omit curly braces for single-line if statements
may result in a change in the styling. So this may be a "redundant"
no-op change to remove this style guidance (and fall through to the Google
style guide) or it may be a change.
The debate is a bit involved. Please ask if you'd like details.
[Audience: developers writing JavaScript code in Chromium]
Let's* remove the redundant entry for single line if-statements in the style
guide
<https://chromium.googlesource.com/chromium/src/+/master/styleguide/web/web.md#Style>.
- Omit curly braces for single-line if statements.
The Google style guide
<https://google.github.io/styleguide/jsguide.html#formatting-braces>
now has the same guidance.
*Err, I'll do it. Just trying to saying it (announcing the intent)
publicly.
--Let's* remove the redundant entry for single line if-statements in the style
guide
<https://chromium.googlesource.com/chromium/src/+/master/styleguide/web/web.md#Style>.
- Omit curly braces for single-line if statements.
The Google style guide
<https://google.github.io/styleguide/jsguide.html#formatting-braces>
now has the same guidance.
*Err, I'll do it. Just trying to saying it (announcing the intent)
publicly.
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google
Groups "Chromium-dev" group.
To view this discussion on the web visit
https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/1e400180-07f6-4034-90bd-5cd59f719fe3%40chromium.org
<https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/1e400180-07f6-4034-90bd-5cd59f719fe3%40chromium.org?utm_medium=email&utm_source=footer>
.
--
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups
"Chromium-dev" group.
To view this discussion on the web visit
https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CANNW_Qp%3D0iVS7PEVMr_ZvgMRL_UnfdZx-wRN-ZKo7QJmJJ_XAA%40mail.gmail.com
<https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CANNW_Qp%3D0iVS7PEVMr_ZvgMRL_UnfdZx-wRN-ZKo7QJmJJ_XAA%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
--
--
Chromium Developers mailing list: chromium-***@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+***@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/9db33cf4-8fe5-4f71-a07f-46b839394fc9%40chromium.org.
--
Chromium Developers mailing list: chromium-***@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+***@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/9db33cf4-8fe5-4f71-a07f-46b839394fc9%40chromium.org.