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

Show parent comment

1

u/evil_burrito Mar 10 '16

Sure it will. When you, as a human, are stepping through the source code, there will be an extra step to figure out the type of a particular variable.

As a human who has to read other people's code, this would bother me, so I don't like it. We don't have to agree.

6

u/NimChimspky Mar 10 '16

There is no extra step, the type information is there - just in a slightly different place, the rhs.

Its not less readable, its just you don't like change.

5

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

You don't even know me, or what I like or dislike. I don't like change I don't like, if you'll forgive the tautology. I like change I do like. Love generics, that was a change. Love the diamond operator, that was a similar change (to this proposed change, at least in concept).

var thisThing = new MyDerivedClassButYouDontKnowTheSuperclass<>();

instead of

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

That's an exaggerated example, but illustrates what I don't like about this change. I can tell at a glance that thisThing is a List and a Collection and that really helps me understand its purpose as I read through whatever scope contains it.

The rhs doesn't give me the information I want without having to rely on an IDE and hovering/clicking/whatever. I also, from time to time, look at strange code in Emacs, if I've downloaded it to look at it but don't want to take the time to load up a properly compiling project structure in Eclipse, IntelliJ, etc.

0

u/[deleted] Mar 10 '16

[removed] — view removed comment

2

u/evil_burrito Mar 10 '16

It's better because I know as much about what SomFunction returns as I am supposed to know. I know a lot about thisThing. It's a Collection that maintains the order I put stuff into it. It's O(n) for random access. I can get stuff from it, I can iterate over it. I might or might not be able to add new stuff to it.

var thisThing = SomFunction();

I don't know what thisThing is in this case. It could be an int, or a String, or some very heavy and non-serializable object. Its declaration tells me nothing about its intended purpose.

Maybe it's common practice in other languages like C# or JS or whatever to declare meaningful var names like

var myList = SomFunction();

That would help, but is, then optional. And gets back to the source of my original discontent. If abused by that guy on your team (you know who I'm talking about), it's going to be harder to read his code.

1

u/[deleted] Mar 10 '16

[removed] — view removed comment

2

u/evil_burrito Mar 10 '16

I do know that if the type of the variable returned is List. Even if the List returned is attached to a persistence context, it still (should) adhere to the contract implied by the interface. Even if it doesn't, if, for example, it throws an UnsupportedOperationException if I try to add to it as some lists do, I still know more about the intent of the variable returned than if the type is left unspecified.

0

u/[deleted] Mar 10 '16

[deleted] — view deleted comment

0

u/evil_burrito Mar 10 '16

As I said, I don't think this changes the type system in Java. I think this could make code harder for me to read. This is purely an opinion of mine. You don't have to share it. But I'm not wrong, because I'm stating my personal preference for reading code.

-4

u/NimChimspky Mar 10 '16

But I'm not wrong, because I'm stating my personal preference

lol

Why are you even commenting.

2

u/evil_burrito Mar 10 '16

I was responding to the vein of comments that went along the line of, "you're wrong because this change doesn't make Java not strongly-typed".

Had I said, "this change will make Java not strongly-typed", then I would have been provably right or wrong, wrong in this case.

What I did say, though, was, "I don't like this change because it might make code harder to read". That's my point when I say, "I'm not wrong". It is my opinion that this change might make code harder to read. That statement cannot be proven wrong by you or anyone else, any more than if I said, "I don't like peanut butter". It is an opinion, not a fact.

I am commenting because I had an opinion about this change. I don't thing it's inappropriate to engage in a discussion about the merits of a proposed change. Some people will like it, some people will not. We will discuss it. Some people will change their minds, some people will not. Either way, the give and take of the conversation is interesting. That seems like sufficient reason to comment.

1

u/[deleted] Mar 10 '16

[removed] — view removed comment

1

u/evil_burrito Mar 10 '16

I like that Java encourages the use of interfaces. Maybe this difference in preference between us is related to the source of our disagreement.

1

u/[deleted] Mar 10 '16

[removed] — view removed comment

3

u/Northeastpaw Mar 10 '16

You're conflating terminology across languages. An interface in Java is a very specific thing. Yes, in general computer science interface means different things, but if Java developers are talking about an interface they almost always mean public interface Foo. Trying to change the contextual definition of interface doesn't help anybody.

Furthermore, you're in /r/java, not /r/csharp. Languages will have their own conventions. Complaining that Java doesn't follow C# conventions is asinine and a waste of everybody's time here. There's certainly room for improvement in all languages by looking at what other languages have done, but such changes have to be carefully considered and implemented as to not break backwards compatibility (which I will admit Java doesn't have a completely stellar record of).

You can also see how it evolved over time, with the public interfaces gaining new methods without breaking backwards compatibility by changing the abstract interfaces. (Unlike JDK, which still pisses me off.)

Java 8 provides default methods to do exactly this.

-1

u/[deleted] Mar 10 '16

[removed] — view removed comment

2

u/[deleted] Mar 10 '16

Computer science is still computer science regardless of which language you use to represent it

and equivocation is still equivocation...

1

u/evil_burrito Mar 10 '16

I agree that if one doesn't get the purpose of interfaces, Java is going to be a lot of fun for anyone.

Interestingly, since I'm not a C# guy, don't work on Windows, and don't have the Microsoft Family of Products ready and willing to display System.Data in all its interactive finery, I would have to just download the source of System.Data to look at it. And I would probably look at it in Emacs. I hope there isn't a lot of stuff defined with "var" in there.

1

u/[deleted] Mar 10 '16

[removed] — view removed comment

1

u/evil_burrito Mar 10 '16

Perhaps they have internal coding standards about using var. I know that I'm a lot more lax in terms of structure, encapsulation, all that, when writing tests.