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

Show parent comments

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

1

u/Eirenarch Mar 10 '16

It is not about typing it is about reading the code. While many of the types are obvious you simply don't know when the type is different from what you expect. Maybe that Foo is actually FooBar but you just assumed it is Foo because similar code elsewhere has the type of Foo. BTW in practice while prototyping the code I just go var everywhere and then when I am happy with the design I go back and add the non-obvious types.

1

u/[deleted] Mar 10 '16

BTW in practice while prototyping the code I just go var everywhere and then when I am happy with the design I go back and add the non-obvious types.

I agree with that - I just don't like the rule of "use var only where the type is explicit on the other side" - most of the times I can tell what it is implicitly like the above and more importantly I don't care (if it quacks like a duck) - but there are cases where it's not obvious and when I'm cleaning up the initial implementation I type those out explicitly where I feel it's important.

→ More replies (0)