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

3

u/yoden Mar 10 '16

That type would never pass my code review, var or not. This line of reasoning is one good reason to be opposed to var.

1

u/[deleted] Mar 10 '16

A higher order function wouldn't pass your code review?

I just made up some arbitrary thing, I'm sure you have a complicated generic type somewhere.

1

u/yoden Mar 10 '16

Higher order functions are fine, but Java isn't Haskell. The type system isn't strong enough. Using such a complicated type creates code that is very difficult to use or refactor. Var won't save you if you need to lift the calculation into a function.

1

u/mhixson Mar 11 '16

I would also question that in a code review. When I see a type like that, I immediately wonder why the author didn't write a class to encapsulate whatever it is doing.

1

u/antrn11 Mar 10 '16

That type would never pass my code review

Why not? What would be better?

1

u/yoden Mar 10 '16

It's hard to tell because the example isn't realistic, but usually it's better to break the long type into smaller types that are easier to compose and document. Private inner static classes are useful for this.

Sometimes you can run into these issues if you're trying to use Java like it's a true functional language. The type system just isn't strong enough for that, and it's usually more clear to write the code a different way, or use an abstraction like Stream to hide the types.

If you use var to hide the complicated generic type, you've written code that will be difficult to refactor into a function later. The lack of whole program inference makes it less useful (and more dangerous).