1

Lombok has a neat feature to use var instead of local variable datatype declarations. Is there a way to "refactor" your entire code base automatically to use var whenever applicable? I'm using IntelliJ IDEA and the Lombok plugin. I didn't find a feature like this.

Also, does it even make sense since var is still considered "experimental" so it might "mess up" the code base when it gets deprecated/is somehow bugged/gets replaced/moved?

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185

1 Answers1

0

No.

Moreover, I wouldn't recommend this.

  • Being explicit is always better so IMHO var is only good when the cost of being explicit is too high. This is only the case with lengthy LHS where the same information is repeated on the RHS.
  • Use diamond operator (or factories). Not that var list = new ArrayList<>() can't work as you removed the type parameters from both sides.
  • Prefer val to var. With val not being longer than var, there's no reason not to use it (unlike when separate final is needed). This way, you can see what variables may change.

I'd bet the Lombok authors would agree with me, so they won't support such a feature. I hope the same holds for the plugin author.

maaartinus
  • 44,714
  • 32
  • 161
  • 320
  • A list can work with e.g. `var list = new ArrayList()` though but you're right, it would break type safety – BullyWiiPlaza Nov 09 '17 at 14:13
  • @BullyWiiPlaza Sure, it can, but writing this is hardly shorter than `List list = new ArrayList<>()` and it removes the information from the more important place. – maaartinus Nov 09 '17 at 15:06
  • 1
    Very soon, java will support `var` out-of-the-box. I see no reason not to start using it already from lombok. – Roel Spilker Nov 10 '17 at 14:05