← Back to context

Comment by whartung

3 days ago

I get the attraction to var, but I, personally, don't use it, as I feel it makes the code harder to read.

Simply, I like (mind, I'm 25 year Java guy so this is all routine to me) to know the types of the variables, the types of what things are returning.

  var x = func();

doesn't tell me anything.

And, yes, I appreciate all comments about verbosity and code clutter and FactoryProxyBuilderImpl, etc. But, for me, not having it there makes the code harder for me to follow. Makes an IDE more of a necessity.

Java code is already hard enough to follow when everything is a maze of empty interfaces, but "no code", that can only be tracked through in a debugger when everything is wired up.

Maybe if I used it more, I'd like it better, but so far, when coming back to code I've written, I like things being more explicit than not.

1. Just because you can use var in a place, doesn't mean you should. Use it where the type would be obvious when reading code like

  var myPotato = new PotatoBuilder.build();

not like

  var myFood = buyFood();

where buyFood has Potato as return type.

2. Even if you don't follow 1, IDEs can show you the type like

  var Potato (in different font/color) myFood = buyFood();

Yes, well expressed. For that case using var is not a wise approach.

It does help when writing:

    var x = new MyClass();

Because then you avoid repetition. Anyways, I don't ever use "var" to keep the code compatible with Java-8 style programming and easier on the eyes for the same reasons you mention.