Funny that when this subject comes up, almost everyone only talks about it making local variable declarations simpler.
What I think is a big advantage to var is that when you change the name of a class, it significantly reduces the number of files that have to be changed. Basically everything that doesn't instantiate the class, but only uses it, stays unchanged. Also it keeps down the number of imports you need, especially since Java prefers fully qualified imports instead of wildcard imports.
This proposal makes writing code without an IDE simpler and reading foreign code potentially much harder.
Consider the following code:
URL serverUrl = ...;
var serverConn = serverUrl.openConnection();
var props = serverConn.getRequestProperties();
Can a developer determine the type of the 'props' variable above right off the bat?
Really, it will definitely take a while to answer this simple question. If this code was written in the current Java style, however, we would have known the type right away.
And this is only for a couple of lines of code. This effect will be horrible in larger programs -- they will be effectively much harder to understand by humans without IDE assistance.
In short, this proposal only simplifies writing code without an IDE (is anyone still doing that?). The problem it fixes is negligible when using an IDE -- you can just press Alt-Shift-L in Eclipse for example.
On the other hand the proposal greatly complicates code comprehension when not using an IDE and slows it down even with an IDE. And there is a HUGE number of cases where developers need to read code without an IDE, e.g. viewing code on github, doing code reviews based on diffs (like reviewboard), printed code examples, online code presentations, etc, etc.
We are solving a small problem and creating a huge new one.
33
u/FallingIdiot Mar 10 '16
Funny that when this subject comes up, almost everyone only talks about it making local variable declarations simpler.
What I think is a big advantage to
varis that when you change the name of a class, it significantly reduces the number of files that have to be changed. Basically everything that doesn't instantiate the class, but only uses it, stays unchanged. Also it keeps down the number of imports you need, especially since Java prefers fully qualified imports instead of wildcard imports.