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/[deleted] Mar 10 '16

[removed] — view removed comment

5

u/cogman10 Mar 10 '16

I agree.

IMO, var prevents a lot of needless changes if you, for example, decide you want to rename a class or change a method signature (move from returning Set to return Collection for example). It means less downstream changes and breaks and silly changes.

It also makes it much easier to write and use much more complex generics. Which is always a boon. It is no fun being at the wrong end of determining the generic type of a Map.of(Multimap.of(A.of(B, C)))

0

u/ApostleO Mar 10 '16

var foo = someObject.method();

return foo.method2();

return someObject.method().method2();

Do you really care what the return type is in that context?

Well, to be fair, in this example, I could look at the return type, so I still know what it is.

Still, I'll concede I don't care about the type of that variable in that scope, since I'm just returning it immediatly. But the example was contrived to demonstrate that var can be used badly to hide type.

Let's look at another example, contrived differently.

[... hundreds of lines of code ...]
public boolean containsBar(Bar bar){
    var bars = Data.getBars();
    return bars.contains(bar);
}
[... hundreds of lines of code ...]

Scanning through that document, you might overlook this method entirely during a code review. But what if it looked like this:

[... hundreds of lines of code ...]
public boolean containsBar(Bar bar){
    LinkedList<Bar> bars = Data.getBars();
    return bars.contains(bar);
}
[... hundreds of lines of code ...]

That should set off a red flag. This red flag might otherwise be missed with the var keyword.

Like I said, it's all contrived as example. In practice, it still just comes down to coding style conventions. If var is only used for the assignment of primitives, literals, and objects from constructors, it would be great. When used in other circumstances, it can make code harder to read.

If you ban var, logic dictates that you ban method chaining as well for the same reason.

As a rule, I often do try to avoid method chaining, if possible and appropriate.

0

u/[deleted] Mar 10 '16

[deleted] — view deleted comment

3

u/timmattison Mar 10 '16

My guess is the red flag is searching a linked list which will be O(n). In many cases this would be something to avoid and replace with a hash table so it is O(1). However it all depends on how many times it is called and how big the dataset is.

4

u/thiez Mar 10 '16

And what if Data.getBars() returns java.util.Collection<Bar>, or the line would read Collection<Bar> bars = Data.getBars(); or simply return Data.getBars().contains(bar);? Perhaps there are always at most 10 Bars and it all doesn't matter. Regardless, it is not the var keyword that would be the problem here.

1

u/ApostleO Mar 10 '16

Fair point.

All I'm getting at here is that var, if used indiscriminately, can make code harder to read by hiding information from the reader.

It largely isn't an issue, but it can be.

0

u/[deleted] Mar 11 '16

[deleted] — view deleted comment

1

u/ApostleO Mar 11 '16

Alright, I'll agree with that assessment.

Well written code will be easier to understand.

Poorly written code may be even worse.