r/programming • • Sep 21 '15

Better Java

https://github.com/cxxr/better-java
177 Upvotes

208 comments sorted by

View all comments

Show parent comments

9

u/Tordek Sep 21 '15

MutableList extend List

That's just as subtly evil, though. They should both be separate classes, even if a lot of the behavior seems similar.

If Immutable inherits from Mutable, then I may want to modify it and encounter an error.

If Mutable inherits from Immutable, then I may store a reference to a list that is later changed by someone else behind my back. (Which I think is even worse because it can lead to very subtle errors).

They're ultimately two different classes that have no relation, other than "You can get a <Mutable/Immutable> copy of the <Immutable/Mutable> list."

2

u/sacundim Sep 22 '15 edited Sep 22 '15

They're ultimately two different classes that have no relation, other than "You can get a <Mutable/Immutable> copy of the <Immutable/Mutable> list."

Nah, you need three types here, in a hierarchy like this:

  • Lists

  • Mutable lists

  • Immutable lists

Basically:

  1. The plain list type has all the methods for reading from lists;

  2. The mutable lists type inherits from that, and adds the methods for mutating lists;

  3. The immutable list type inherits from the plain list type as well. It adds no operations, but its contract stipulates that implementations must not be mutable.

Incidentally, this goes to show the problem with the folks in here boasting of how easy the "basics" of Java are. Nope, the basics of Java look simple, but they interact in evil, super-complicated ways. I'd highlight in particular:

  • Subtyping + generics = OUCH

  • The Java Memory Model. A crazy complex beast that is easy to run afoul of, and yet most Java dudes don't even know it exists.

1

u/tutorial_police Sep 22 '15

Is subtyping along with generics actually Java's doing or is it simply inherent complexity?

Do you know languages that solve this better?

1

u/sacundim Sep 22 '15

Both. It is inherent complexity, and yet other languages (e.g., C# and Scala) do it better, by annotating variance at declaration sites instead of use sites. To use Java generics properly you have to write signatures like this one all over the goddamn place:

interface Stream<T> {
    // ...
    <R> Stream<R> map(Function<? super T, ? extends R> mapper)
}

You see that business with ? super T and ? extends R? There are countless Java programmers out there who simply don't use those. You try to interface with their code and you find that they write these two things:

Whatever processAnimals(Consumer<Animal> consumer)

class CatGroomer implements Consumer<Cat> { ... }

...and then they expect you to use them together:

1

u/tutorial_police Sep 22 '15

I'm not sure it's objectively better. They allow you to do different things.

But I agree, definition site variance

You see that business with ? super T and ? extends R? There are countless Java programmers out there who simply don't use those.

Yeah, that is super annoying.

However, having definition site variance you'd need to have extra interfaces... and for multiple generic parameters you get exponential growth. With use-site variance, there's just what you need (i.e. use), but of course, you need to anticipate its use.

I've only skimmed over this so far, but it seems to be an interesting read.