Skip to content

Enforce var vs FullTypeName in java 11 #1532

Open

Description

@nedtwigg

The informal Spotless styleguide has been "any style that matters is enforced by spotlessApply". Now that we're compiling against Java 11 world, there's a big new degree of freedom, which is when to use var or not.

I'd like to adopt a style which is "you must always use var, unless the line has a comment".

// before spotlessApply
String example = "blah";

// after spotlessApply
var example = "blah";
// before spotlessApply
String example = "blah"; // keep type

// after spotlessApply
String example = "blah"; // keep type

I'd like it to be a discrete step, ala

spotless { java {
  gjf() // or whatever the user is currently using
  alwaysUseVarUnlessCommented()
} }

But if there's a way to configure an existing formatter, GJF, palantir-java-format, or eclipse, that would work too.

Suggestions and PRs welcome!

Activity

  1. pinned this issue on Jan 26, 2023
  2. jbduncan commented on Jan 27, 2023

    @jbduncan
    Member

    This sounds like a job for Refaster or OpenRewrite. If either of those could be integrated, then this issue should be easier to implement.

  3. jbduncan commented on Jan 27, 2023

    @jbduncan
    Member

    Alternatively, on seeing comment #1379 (comment), I wonder if Eclipse has a reusable method we could use. 🤔

  4. agreedSkiing commented on Jan 27, 2023

    @agreedSkiing

    Alternatively, on seeing comment #1379 (comment), I wonder if Eclipse has a reusable method we could use. 🤔

    there was commit it seems don't know how to use it though eclipse-jdtls/eclipse.jdt.ls@e8e3c5e#diff-4b8c27e96cf0ef72f56e789c7366e6f522fb49bb0caf9dba777142d6417c9a45

  5. nedtwigg commented on Jan 27, 2023

    @nedtwigg
    MemberAuthor

    Awesome! That jdt.ls action should be easy to integrate once #1524 gets merged.

  6. nedtwigg commented on Mar 7, 2023

    @nedtwigg
    MemberAuthor

    Thanks to @blacelle, we can now use cleanthat to enforce var usage. You can see how that changes the code here.

    My initial thought was that it was important to have an easy way to keep the var, I liked the idea that if the line or any line of the statement had a // or /* then the var would not happen automatically. Seemed like an intuitive thing that would be easy for end-users to understand, and it was easy to "self-document" with alwaysUseVarUnlessCommented().

    After seeing @blacelle's PR, I'm wondering if it's maybe not necessary to have a way to opt-out, and maybe making a specific alwaysUseVar rule is unnecessary - users can get it as part of an Eclipse step or a CleanThat step. Thoughts?

  7. unpinned this issue on Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions