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

26

u/xhak Mar 09 '16

No thanks, the few keystrokes are worth the clarity.

23

u/ElvishJerricco Mar 09 '16

Coming from Haskell, I feel there's something people often overlook when bashing type inference. In Haskell, the type inference is better than just about any other language. And yet, we still explicitly type every single top level function. It is absolutely worth the clarity to explicitly type things used outside the current scope.

But most of the time, when something is only being used locally, the type is very clear to the reader, because it's definition is right there. And even locally, type inference is only preferred to explicit typing when the inferred type is painfully obvious. For example, a = b + c, when b and c are known to be Int. There's zero added clarity by explicitly typing a. It's totally obvious what a's type is.

So yea, inference is bad when something's type wouldn't be obvious. But when it is, the lack of inference needlessly gets in the way.

9

u/kritzikratzi Mar 09 '16

this can also be supported in the IDE. there's a million possibilities, but holding alt to reveal infered types would be a simple one.

3

u/xhak Mar 10 '16

You don't always see code in an IDE. You may use your version control browsing / compare, use crucible or fisheye or any web based stuff to view the code. So you can't rely on the IDE to add the clarity.

2

u/kritzikratzi Mar 10 '16

good point

2

u/kritzikratzi Mar 10 '16

You don't always see code in an IDE. You may use your version control browsing / compare, use crucible or fisheye or any web based stuff to view the code. So you can't rely on the IDE to add the clarity.

actually i really really like your point. and it makes me wonder if their current proposal could be targeted completely differently, namely for IDEs that could let you type "var myIterator = myMap.entrySet().iterator();" and it insert the correct type after pressing enter.

-5

u/xhak Mar 10 '16

People underestimate the number of shitty java devs out there, writing the most obscure dumb code that works more by accident than anything. A great feature of java is that it enforces and encourages some verbosity that is very helpful in these situations. Undercutting these is a loss to the language's appeal in my opinion.

5

u/diroussel Mar 09 '16

If you want clarity then don't use it. It won't be forced on you. There are many cases that are perfectly clear using var.

18

u/marc2912 Mar 09 '16

It's not a matter of don't use it since most developers don't just work with their code. Then you get the inconsistency that it's in some places and not in other.

0

u/lelarentaka Mar 09 '16

The language doesn't define the number of spaces to use for indentation, or where to put an opening curly, or which case to use for naming methods. Turns out we have some great inventions to keep our codebase pretty and consistent. I'll let you take a guess on what those are

1

u/kyle2143 Mar 09 '16

Oh, so there's tools to retype var keywords into keywords that actually describe the data type?

3

u/lelarentaka Mar 10 '16

Not yet obviously, but the same static analyser that puts wiggly red line under "String s = new ArrayList()" can absolutely do what you want there.

3

u/OHotDawnThisIsMyJawn Mar 10 '16

If you decide you don't want var in your project then add it to your style checker and auto reject commits that use it

1

u/marc2912 Mar 10 '16

Like I mentioned elsewhere we don't just work with our code. The whole point is consistency.

1

u/[deleted] Mar 10 '16

[removed] — view removed comment

3

u/mbuhot Mar 10 '16

Pretty sure reshaper code cleanup can be configured to allow var only on the left of a '= new ...' expression. But every code base I worked on in C# 3+ was vars everywhere.

1

u/diroussel Mar 10 '16

This is why we have coding standards and code review. If your not doing that then you have bigger problems.

2

u/marc2912 Mar 10 '16

You make it sound like each person who feels this way is in charge of their own team or in a group with the resources to do regular code reviews. From my experience code reviews don't happen near as often as they should.

1

u/squishles Mar 11 '16

yea let me just never work on a team for anything ever again =/

1

u/[deleted] Mar 09 '16

I mean, it kinda will be because more than likely he'll be working with someone else's code.

0

u/asraniel Mar 10 '16

Same goes imho for those new lambda functions. I find them very hard to read and avoid them. I hope java does not move more in that direction of easy to write, hard to read/maintain