r/programming • • Mar 09 '16

var keyword proposed for Java

http://openjdk.java.net/jeps/286
250 Upvotes

395 comments sorted by

View all comments

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 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.

11

u/G_Morgan Mar 10 '16

It really depends. A lot of C# projects have a rule to only use var where there is a straight up class instantiation or cast. The idea is to have at least some type definition somewhere near the declaration.

The purpose of var is to stop bullshit like "X<Y> foo = new X<Y>();"

7

u/Eirenarch Mar 10 '16

I came up with this rule by myself and found out that many people came to the same conclusion. I only use var when the right hand side expression is either a constructor call or a literal. Basically the type should be obvious by the statement.

var customers = GetCustomers()

is not a good idea in my opinion

7

u/Ravek Mar 10 '16

IEnumerable<Customer> customers = GetCustomers(); doesn't tell you a single thing you need to know that var didn't already.

14

u/Eirenarch Mar 10 '16

Oh yes, it does tell me. It tells me I have an IEnumerable<Customer> rather than IEnumerable<CustomerDto> or PagedList<Customer> or something like this.

-1

u/[deleted] Mar 10 '16 edited Mar 10 '16

IDE tells you that. Documentation/go to definition tells you that. In most cases it's irrelevant anyway (obvious from use when reading or intellisense when working on it)and it's just noise.

And don't even try to argue "what about people who don't use an IDE" - sorry but if your editor doesn't support features mentioned above (and even Vim does) it's not fit for development with these languages - their highest value proposition is powerfull tool integration.

3

u/Eirenarch Mar 10 '16

I don't want to have to move the mouse over the var keyword or something like that. Also I have tried that empirically with interns. While it is true that interns are far worse than experienced programmers I have seen them make a bunch of mistakes from not knowing the type of the variable under var (most often IEnumerable vs IQueryable and examples like the one I gave). They wrote the code and a week later I ask them to tell me the type of the variable without pointing with the cursor and they fail. The real problem is they don't even know they have to look it up otherwise there would be no bug to begin with.

1

u/[deleted] Mar 10 '16 edited Mar 10 '16

The real problem is they don't even know they have to look it up otherwise there would be no bug to begin with.

Do you think typing it out really helps here ? I guess because they have to type out IQueryable and then they figure out they don't understand what that type is and won't just treat it like IEnumerable ? I can see the value in that.

But in general I think more of my code falls in the above category of "Foo foo = bar.Foo;" and typing out Foo is just pointless verbosity even when it's a result of a method call/getter

→ More replies (3)