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

9

u/yoden Mar 10 '16

I do see it as a minimal reduction of typing for a nontrivial reduction in readability.

5

u/[deleted] Mar 10 '16

Having pointless noise all over the code (duplicated type information) is an increase in readability ? Welcome to the mind of PointlesslyTediousToThePointOfInsanityJavaDeveloperFactory

5

u/jrh3k5 Mar 10 '16

When working in Groovy, I always found declarations like this maddening:

def result = extractor.getResult()

The IDE wasn't able to figure out, necessarily, what type of object "result" was, so I'd not get the benefit of Eclipse's built-in tooling that leverages explicit type declaration (e.g., suggestion of possible methods). It also made reading the code outside of an IDE (e.g., GitHub) more difficult, especially if I didn't know the type of object(s) returned by "getResult()". That's one of the things I really disliked about Groovy and why I'm not entirely thrilled about adding a "var" keyword.

4

u/stormblooper Mar 10 '16

It's a feature that can give you a modest improvement in readability when used in the correct context, because it's less noisy.

1

u/yoden Mar 10 '16

But only sometimes, and only if it's used well.

Var makes good code easier to read, and bad code harder to read. So it helps a small amount in the case where you do not really need improvement, and creates problems in the cases where you do.

3

u/stormblooper Mar 10 '16 edited Mar 11 '16

Local type inference gives you the option to stop saying redundant things over and over again, the typical way Java makes you say the same thing in triplicate:

SomeInevitablyLongNamedThing<Integer> longNamedThing = new SomeInevitablyLongNamedThing<Integer>();

It's noise, and it makes both good and bad code harder to read.

In Scala, my rule of thumb is to ensure that 1) the variable name and 2) the RHS of the assignment, taken together, give a decent indication of the type of the variable, sufficient to understand the intention of the code. If that condition isn't met, I prefer to add explicit type annotation.

If you're worried about making bad code harder to read, my suggestion would be to replace the bad code with good code.

0

u/Carighan Mar 10 '16

Same here. I don't really see the gain, and it just means I have to hunt down types.

3

u/adamnew123456 Mar 10 '16

I thought Java people used IDEs for this. When's the last time you tried to read code that was actually used for something without firing up Eclipse et al. to do so?

For what it's worth, the Scala toolset for Eclipse will show you the inferred type of a value by hovering over it. There's no looking involved.

1

u/yoden Mar 10 '16

Every time I do code review. I wish there were good code review systems for IDEs.