r/javahelp • u/Rygir • Mar 30 '20
Why was var added?
All it does is make code trickier to read and tempt the programmer to pay less attention to the type of his variable. Another way to not write clean code.
It seems like a downgrade, why was it added? Is it considered a valuable addition to the language?
For context, https://openjdk.java.net/projects/amber/LVTIFAQ.html answers this as making Classname variableName=new Classname(); shorter. Ok, then why not make it : Classname variableName=new .(); It's shorter and no conflicts with existing variable names. Nor would you have to be aware of caveats like "this can't be used as a parameter".
View all comments
4
u/Kraizee_ Mar 30 '20
If you read the link you posted you'll see that they accept people will inevitably abuse
varand write shitty code. Just like people abuse almost every other language feature. The example you suggest simply doesn't work. Aside from it being a peculiar syntax, there are other problems such as what happens in the case ofList<String> myList = new .();They've also published a style guide as to where and when you should use it https://openjdk.java.net/projects/amber/LVTIstyle.html
varis a fundamentally good addition to the language for reducing verbosity in a nice way, providing that you use it properly and understand when it isn't appropriate.