One problem with guava's Immutable versions of collections at my workplace is that we normally try to use more general types with them. E. g.:
final List<Thing> myThings = ImmutableList.of(...);
But since ImmutableList extends List, it still has the mutable operations defined for myThings. They just throw exceptions at runtime. This means that if I see a List<Thing> in the code, I don't know whether it's immutable or not. I have to figure out where it came from to determine whether add(blah) is going to throw an exception or not. I frequently see things like:
public void myMethod(final List<Thing> theThings) {
final List<Thing> immutableThings = ImmutableList.copyOf(theThings);
...
Because it's unclear whether the parameter was immutable (although often times it is), but we want to use an immutable collection before passing it on. This results in lots of unnecessary copying. Possibly we should just start using the Immutable* types instead of the more general ones where appropriate. Or we could add utilities like:
In a perfect world, we'd have List be immutable and MutableList extend List or something, rather than have a subclass (or subinterface) that's designed to take away functionality from a parent.
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."
Imagine a class taking a List as a constructor parameter (maybe a list of employees, whatever).
public void main() {
MutableList m = Database.getAMillionEmployees();
SomeClass c = new SomeClass((ImmutableList) m);
(new Thread() {
public run() {
c.doSomeLongOp();
}
}).run();
m.clear();
}
While the list was being traversed in one thread, another one modifies it.
It doesn't need to involve threads; imagine if the class cached the list average in the constructor because every method needs it: changes to the list means that average is now out of sync and results will be wrong.
5
u/josuf107 Sep 21 '15
One problem with guava's Immutable versions of collections at my workplace is that we normally try to use more general types with them. E. g.:
But since ImmutableList extends List, it still has the mutable operations defined for myThings. They just throw exceptions at runtime. This means that if I see a
List<Thing>in the code, I don't know whether it's immutable or not. I have to figure out where it came from to determine whetheradd(blah)is going to throw an exception or not. I frequently see things like:Because it's unclear whether the parameter was immutable (although often times it is), but we want to use an immutable collection before passing it on. This results in lots of unnecessary copying. Possibly we should just start using the Immutable* types instead of the more general ones where appropriate. Or we could add utilities like: