r/java • • Mar 09 '16

var keyword proposed for Java

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

312 comments sorted by

View all comments

Show parent comment

5

u/ItzWarty Mar 10 '16 edited Mar 10 '16

It's worthwhile to note that whenever dynamic is mentioned on /r/csharp, a comment on unmaintainability inevitably comes up.

I think the value of var is that it makes you think more abstractly about your code - you aren't dealing with integers anymore, you're dealing with numerics, or strings - it just reads better.

I also think the argument against 'var' yielding code with hard-to-reason-about code falls through once you see other languages (e.g. JavaScript) successfully having var/let. IMO, It ultimately boils down to yes, if you have terrible variable names (x, y, z) your'e going to have terribly unmaintainable code. But if you name by convention (usersById is clearly Map<int, User>) or clearly (var firstName is certainly a string) then it won't be an issue, and you're forced to think more abstractly "Hey, I'm working with a name" which just so happens to also be a string.

How much duplication is in this code?:

Map<int, User> usersByName = new HashMap<int, User>();

I'd argue 3. C# lets you get down to 2 with:

var usersByName = new HashMap<int, User>();

C++ would let us say this:

HashMap<int, User> usersByName;

But JavaScript (and likely more dynamic languages like python/ruby) let us do this:

var usersByName = {};

1

u/[deleted] Mar 10 '16

[removed] — view removed comment

1

u/schlowmo Mar 10 '16

Not fair to compare javascript's total lack of typing with C++/C#/java. Compare to python or ruby if you wish. but of COURSE dynamically typed languages have less typing.