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

74

u/evil_burrito Mar 09 '16 edited Mar 10 '16

I think one of the problems of untyped or weakly-typed languages is that they can be difficult to debug or understand somebody else's code.

I'm a fan of strong typing. I doubt that puts me in the majority here.

Edit: I'm simply stating a personal preference here: I think this will decrease the readability of source code. We don't all have to agree.

Edit: I can see how my original comment was poorly worded. I didn't mean to say, "I think this change will make Java less than a strongly-typed language", I meant to say, "I think this change might make Java read like less than a strongly-typed language".

16

u/stepancheg Mar 09 '16 edited Mar 09 '16

Seems like you are mixing weakly/strongly typed languages and dynamically/statically typed.

Anyway, with var Java is still statically and strongly typed.

Strong means "17" is String, and cannot be viewed as int.

Static means that even without explicit type each var still has a type, and you CANNOT write:

var i = 19;
i = "there";

3

u/evil_burrito Mar 10 '16

No, I'm not. My complaint isn't that it makes Java a weakly-typed language. My complaint is that it makes Java read like a weakly-typed language, which makes code other people wrote harder to understand.

0

u/[deleted] Mar 10 '16

[deleted]

0

u/evil_burrito Mar 10 '16

That's a Visual Studio thing or something similar? I don't work on Windows.

Regardless, I'm talking about the ability to quickly grok unfamiliar code. I believe that having type information that isn't obvious at a glance would be a detriment to me. Thus, I don't like this proposal.

6

u/pjmlp Mar 10 '16

That is what IDEs are for.

3

u/evil_burrito Mar 10 '16

Yes, that is what IDEs are for, but for reading strange code, it's not always set up in a full project that compiles and has all the dependencies, etc, that would be necessary for an IDE to function.

From time to time, I'll have to look at the source for an included library for which I don't want to take the time to set up a complete project. I think this change will impair my ability to quickly read through strange source and understand what's going on. I don't want to have to hover or click or whatever.

I find this to be more readable:

List<Something> thisThing = new MyDerivedClassButYouDontKnowTheSuperclass<>();

than this:

var thisThing = new MyDerivedClassButYouDontKnowTheSuperclass<>();

Therefore, I like the former and don't like the latter.

1

u/stepancheg Mar 11 '16

MyDerivedClassButYouDontKnowTheSuperclass should not implement List. Something like ConcurrentCopyOfWriteMySuperLinkedList could, but in that case it is obvious to reader that it is list, further clarification is not necessary.

2

u/evil_burrito Mar 11 '16

Your argument is to trust the developer to name their class properly. The thrust of my argument is that I do not trust other developers to write code that I find readable.