Styleguide verdict for "var" type inference

772 views
Skip to first unread message

Nate Fischer

unread,
Oct 25, 2022, 6:22:28 PM10/25/22
to java
Hi all,

Java introduced the "var" keyword to allow declaring a variable without explicitly specifying the type. The compiler determines the appropriate type using type inference. For those familiar with C++, this is the Java version of C++'s "auto" keyword. I ran a quick check in my local checkout and this compiles and runs fine on Android M and above.

I see some usage of this keyword in our code base (codesearch link, one example), but I don't see it mentioned in our styleguide, the AOSP style guide, or the public Google Java styleguide. Is it permitted to use this keyword in chromium?

One scenario where I'd like to use this keyword would be for the try-with-resources pattern. I think this is nice when the resource returned in the try statement is not called directly. Example:

    try (var ignored = StrictModeContext.allowDiskWrites()) {
        writeToDisk();
    }


Is anyone strongly opposed to allowing this keyword for limited usage? Is anyone strongly in favor of this usage, or perhaps thinks this should be allowed more broadly?

Regards,
Nate Fischer | Software Engineer | ntf...@google.com

Andrew Grieve

unread,
Oct 25, 2022, 9:19:57 PM10/25/22
to Nate Fischer, java
I sent a message in August about var being usable, but didn't think about updating the style guide for it.

I'm a bit surprised it's not mentioned in Google's style guide. I did find an OpenJDK style article about it, which I think we could just link to.

--
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/CACiwn8ZH46dJaBXv67SAb5j05X5Mmct2yFp16A4xu-9sgPyeZg%40mail.gmail.com.

Peter Conn

unread,
Oct 26, 2022, 6:48:50 AM10/26/22
to java, Andrew Grieve, java, Nate Fischer
As Nate mentioned, var is equivalent to C++'s auto, which we do have guidance for in the Google C++ Style Guide:

"The fundamental rule is: use type deduction only to make the code clearer or safer, and do not use it merely to avoid the inconvenience of writing an explicit type. When judging whether the code is clearer, keep in mind that your readers are not necessarily on your team, or familiar with your project, so types that you and your reviewer experience as unnecessary clutter will very often provide useful information to others. For example, you can assume that the return type of make_unique<Foo>() is obvious, but the return type of MyWidgetFactory() probably isn't."

I quite like it - I'd suggest our guidance be along similar lines.

Peter C

Tommy Nyquist

unread,
Oct 26, 2022, 6:30:22 PM10/26/22
to Peter Conn, java, Andrew Grieve, Nate Fischer
On Wed, Oct 26, 2022 at 3:48 AM Peter Conn <pec...@chromium.org> wrote:
As Nate mentioned, var is equivalent to C++'s auto, which we do have guidance for in the Google C++ Style Guide:

"The fundamental rule is: use type deduction only to make the code clearer or safer, and do not use it merely to avoid the inconvenience of writing an explicit type. When judging whether the code is clearer, keep in mind that your readers are not necessarily on your team, or familiar with your project, so types that you and your reviewer experience as unnecessary clutter will very often provide useful information to others. For example, you can assume that the return type of make_unique<Foo>() is obvious, but the return type of MyWidgetFactory() probably isn't."

I quite like it - I'd suggest our guidance be along similar lines.

+1. I also like this guidance.
 

Peter C
On Wednesday, October 26, 2022 at 2:19:57 AM UTC+1 Andrew Grieve wrote:
I sent a message in August about var being usable, but didn't think about updating the style guide for it.

I'm a bit surprised it's not mentioned in Google's style guide. I did find an OpenJDK style article about it, which I think we could just link to.

On Tue, Oct 25, 2022 at 6:22 PM 'Nate Fischer' via java <ja...@chromium.org> wrote:
Hi all,

Java introduced the "var" keyword to allow declaring a variable without explicitly specifying the type. The compiler determines the appropriate type using type inference. For those familiar with C++, this is the Java version of C++'s "auto" keyword. I ran a quick check in my local checkout and this compiles and runs fine on Android M and above.

I see some usage of this keyword in our code base (codesearch link, one example), but I don't see it mentioned in our styleguide, the AOSP style guide, or the public Google Java styleguide. Is it permitted to use this keyword in chromium?

One scenario where I'd like to use this keyword would be for the try-with-resources pattern. I think this is nice when the resource returned in the try statement is not called directly. Example:

    try (var ignored = StrictModeContext.allowDiskWrites()) {
        writeToDisk();
    }


Is anyone strongly opposed to allowing this keyword for limited usage? Is anyone strongly in favor of this usage, or perhaps thinks this should be allowed more broadly?

Regards,
Nate Fischer | Software Engineer | ntf...@google.com

--
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/CACiwn8ZH46dJaBXv67SAb5j05X5Mmct2yFp16A4xu-9sgPyeZg%40mail.gmail.com.

--
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.

Nate Fischer

unread,
Oct 31, 2022, 11:17:19 PM10/31/22
to Tommy Nyquist, Peter Conn, java, Andrew Grieve
It sounds like most folks are generally in favor of allowing this language feature (with the accompanying guidance). I've drafted an addition to the style guide at https://crrev.com/c/3996182 (the rendered copy of this CL patchset is https://chromium.googlesource.com/chromium/src/+/refs/changes/82/3996182/1/styleguide/java/java.md).

Is anyone opposed to allowing this feature in general? If there are no objections, I will send the CL out for review to one of the styleguide owners.
Nate Fischer | Software Engineer | ntf...@google.com

Nate Fischer

unread,
Nov 7, 2022, 1:21:54 PM11/7/22
to Tommy Nyquist, Peter Conn, java, Andrew Grieve
Having not yet heard objections, I have landed https://chromium-review.googlesource.com/c/chromium/src/+/3996182 and the documentation is now live: https://chromium.googlesource.com/chromium/src/+/main/styleguide/java/java.md#Java-10-language-Features

Please reach out if there are concerns and I'll do my best to address them.

Nate Fischer | Software Engineer | ntf...@google.com

Reply all
Reply to author
Forward
0 new messages