Comment by guax

Comment by guax 3 days ago

4 replies

To me var is what makes modern java somewhat readable and more bearable. It was always a joke that it takes too long to write anything in java because of the excessive syntax repetitions and formalities. To me that joke is heavily based on a reality that modern Java is tackling with this quality of life features.

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.

  • CrimsonRain 3 days ago

    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();
  • nunobrito 3 days ago

    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.
mi_lk 3 days ago

I have the opposite feeling. var makes it easier to write but harder to read/review. Without var you know the exact type of a variable without going through some functions for example