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.

-1

u/doom_Oo7 Mar 10 '16

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

No, because it might return a type that is implicitely convertible to CustomerValidationResponse and where the conversion has side-effects. So you still have to check it, while in the var case you know that the variable is of the type of the return of the function.

2

u/id2bi Mar 10 '16

There are implicit conversions in Java?

1

u/doom_Oo7 Mar 10 '16

I thought that there were but it seems that there aren't. Oh well !

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.

1

u/immibis Mar 10 '16

I have been of the opinion in c++, that if the type isn't obvious then you shouldn't use auto.

Unfortunately, the person whose code you're trying to read does not have that opinion.

6

u/kqr Mar 10 '16

"But you can use it to write bad code" is always the argument against introducing a language feature. It doesn't matter which feature. Someone will say it's a bad idea because you can write bad code with it.

Guess what? You can do that with any feature. It's a bad argument.

2

u/immibis Mar 10 '16

"It encourages bad code" is a reason against introducing a language feature. Even for things that are controversially bad (i.e. not everyone agrees they're bad).

See some of the other comments on this very post.

3

u/kqr Mar 10 '16

Sure, that's a better argument. I guess we disagree on the degree to which local type inference encourages bad code.

0

u/G_Morgan Mar 10 '16

Yeah wherever I've seen it used there has been a rule that var is only to be used when followed by:

  1. A straight constructor call.

  2. A factory method call.

  3. A cast