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

74

u/evil_burrito Mar 09 '16 edited Mar 10 '16

I think one of the problems of untyped or weakly-typed languages is that they can be difficult to debug or understand somebody else's code.

I'm a fan of strong typing. I doubt that puts me in the majority here.

Edit: I'm simply stating a personal preference here: I think this will decrease the readability of source code. We don't all have to agree.

Edit: I can see how my original comment was poorly worded. I didn't mean to say, "I think this change will make Java less than a strongly-typed language", I meant to say, "I think this change might make Java read like less than a strongly-typed language".

73

u/Northeastpaw Mar 09 '16

This isn't about changing Java to a weakly typed language, though. Java currently determines types via RHS (with a bit of LHS scanning for the diamond operator). If you instantiate a variable with a concrete type on the RHS, why do you need to declare its type on the LHS as well? You've already stated what you want when you instantiated it.

This is simply syntactic sugar to switch this Map<String, List<Class<?>>> foo = new HashMap<>(); for this var foo = new HashMap<String, List<Class<?>>>();.

26

u/evil_burrito Mar 09 '16

I do understand that.

My point was that:

var foo = whatevs

is more difficult for humans to read while going through somebody else's code.

10

u/jewdai Mar 10 '16

Java developer turned C# dev here:

Generally C# encourages you to use var when its clear what the type is.

var x = new MySpecialObject();
var y = "some string";
var z = Factory.GenerateMyObject();

Additionally, because its implicit typing your intellisense will still work even when it's not clear what the object type is.

Part of C# take on it is that you cannot RETURN implicitly typed object.

HOWEVER

C# supports dynamic typing. Its generally used in very special circumstances and it makes you incredibly aware that something could go wrong so dont use it unless you REALLY, REALLY have to. You can even return dynamic as a type.

How this differs from var is that it isnt implicit, its type is UNKNOWN at compile time and only once the code is run can it check if .GetValue() is a method of that type.

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.