Reconsidering Java optional-brace policy

12 views
Skip to first unread message

Alex Cooper

unread,
Mar 28, 2023, 2:44:19 PM3/28/23
to ja...@chromium.org
Hey Java@,

The Chrome C++ style guide was recently updated to remove divergence from the main Google C++ style guide and removed the language allowing braces to be optional. (Disucssion Here).
It looks like the Chrome Java style guide still allows braces to be optional if the entire thing is on one line, while the Google Java style guide also requires braces all the time. 

Given the recent C++ changes, I'm wondering if we should make a similar update to the Chrome Java style to always require braces?

Thanks,
Alex

Alex Cooper

unread,
Mar 28, 2023, 2:46:05 PM3/28/23
to ja...@chromium.org, Peter Boström
CC'ing @Peter Boström who championed the C++ changes.

Tommy Nyquist

unread,
Mar 28, 2023, 4:47:24 PM3/28/23
to Alex Cooper, ja...@chromium.org, Peter Boström
Up until now, we've been following the Android style guide when it comes to Java code in Chromium, which allows one-line statements to be written without braces.

There are many differences between the Java style guide from Android and Google, so I don't know if it makes sense to bring in this change in particular to Chromium.

I think the policy that it all has to be all on a single line, both the conditional and the statement, makes it less prone to accidental use, and as such would be OK to keep as is in my opinion.

--
You received this message because you are subscribed to the Google Groups "java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/java/CAGOLbz0UEye5UCc6NA-CUj5hvMuZSsffvCqxWY3UWH8MWiLmsg%40mail.gmail.com.

Peter Conn

unread,
Mar 29, 2023, 5:46:35 AM3/29/23
to java, Tommy, ja...@chromium.org, Peter Boström, Alex Cooper
We do use this quite a lot in Java, especially for guard statements like - we have over 5,000 uses of this pattern alone:

if (listener == null) return;

Personally I'm in favour of them - letting something take up only 1 line when otherwise it would be 3, and I agree with Tommy that they're less error prone when everything has to be on the same line.

Reply all
Reply to author
Forward
0 new messages