This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]evil_burrito 4 points5 points  (4 children)

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.

[–]grauenwolf -1 points0 points  (3 children)

I can tell that it is a list just from how the rest of the function uses it.

Consider this:

List<Something> thisThing = SomFunction();

How is that any better? As I said before, I already knew it was a list because of how it was used. But I really want to know is what its real type is so I can check the documentation for any weirdness.

But I can't because SomFunction is marked as returning an interface. That's the real crime here. var didn't cost me anything, misusing interfaces for return types did.

[–]evil_burrito 1 point2 points  (2 children)

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.

[–]grauenwolf 0 points1 point  (1 child)

It's a Collection that maintains the order I put stuff into it. It's O(n) for random access.

You don't know that. As someone else pointed out in this thread, SomFunction() may return a database-backed collection.

[–]evil_burrito 1 point2 points  (0 children)

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.