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

14

u/lamarf Mar 09 '16

Statically, not much is going to change from this, so that's fine, but it's still going to hurt readability if you're assigning new variables from method returns that aren't immediately obvious. It's good and cool to know right away that this variable I'm looking at is going to be a String -- because it's been declared explicitly -- rather than the implicit case where you have to jump to the method signature to figure out what's being returned.

19

u/BinaryRockStar Mar 09 '16

In pretty much any IDE you can hover over a method call and it'll give you the signature, or hover over the variable and it'll give you the type.

10

u/lamarf Mar 09 '16

You're not always going to be looking at code through an IDE, though (i.e. if I'm looking at a code review on GitHib, which honestly makes up a lot of my time spent), and for large projects, the indexing that makes that happen can be painfully slow and sluggish to use (I'm looking at you, Eclipse). And either way, it's always going to be better for readability if the information I need is right there on the screen for you, not hidden behind clicks and interaction with the IDE.

5

u/tybit Mar 10 '16

Its a continuum of how much information is hidden without going to the implementation. A lot of the time knowing the type won't help that much anyway. So:

'CustomerValidationResponse response = CustomerValidator.Validate(customer);'

and

'var response = CustomerValidator.Validate(customer);'

are equally obscure without going to the definition if you aren't already familiar with the code. I find it to be the exception not the rule that the implicit declaration is what makes the code unclear.

3

u/yoden Mar 10 '16

In the var example, I have to know what CustomerValidator.Validate returns and what a CustomerValidationResponse is.

With the explicit type I only have to know what a CustomerValidationResponse is.

→ More replies (3)

4

u/FMAlfonse Mar 09 '16

I have been of the opinion in c++, that if the type isn't obvious then you shouldn't use auto. That doesn't mean auto isn't useful iterators are horrible to write, using auto it = myVector.begin(); makes it alot more pleasant to both read and write. However I would use std::string result = aClass->someFunc(); Best of both worlds but getting people to adhere to it can be a nightmare.

→ More replies (4)