Set<Long> set = new HashSet<Long>();
set.add(10L);
if (set.contains(10)) {
// we won't get here!
}
We're asking if the set contains the Integer ten; it's an "obvious" bug, but the compiler won't catch it because Set.contains() accepts Object. Isn't this stupid and evil?
A popular myth is that it is stupid and evil, but it was necessary because of backward compatibility. But the compatibility argument is irrelevant; the API is correct whether you consider compatibility or not. Here's the real reason why.
Let's say you have a method that wants to read from a Set of Foos:
public void doSomeReading(Set<Foo> foos) { ... }
The problem with this signature is it won't allow a Set<SubFoo> to be passed in (where SubFoo is, of course, a subtype of Foo).
To preserve the substitutability principle, any method that wants to read from a set of Foos should be equally able to read from a set of SubFoos, so let's tweak our signature:
public void doSomeReading(Set<? extends Foo> foos) { ... }
Perfect!
But here's the catch: if Set.contains() accepted type E instead of type Object, it would now be rendered completely unusable to you inside this method body!
That signature tells the compiler, "don't let anyone ask about containment of an object unless you are damn sure that it's of the exact right type." But the compiler doesn't know the type -- it could be a Foo, or SubFoo, or SubSubFoo, or who knows what? Thus the compiler would have to forbid everything -- the only safe parameter to a method like this is null.
This is the behavior you want for a method like Set.add() -- if you can't make damn sure of the type, don't allow it. And that's why add() accepts only type E while contains() accepts anything.
So the distinction I'm making is between read methods and write methods, right? No, not exactly -- notice that Set.remove() also accepts Object, and it's a write method. The real difference is that add() can cause "damage" to the collection when called with the wrong type, and contains() and remove() cannot.
Uniformly, methods of the Java Collections Framework (and the Google Collections Library too) never restrict the types of their parameters except when it's necessary to prevent the collection from getting broken.
So what to do about this vexing source of bugs, as illustrated at top? Well, when I typed that code into IntelliJ, it flagged a warning for me right away. This let me know to either fix the problem or add an annotation/comment to suppress it. Problem solved.
Static analysis plays an extremely important role in the construction of bug-free software. And the very best kind of static analysis is the kind that pops up in your face the second you write something questionable.
The moral of the story: if you're not coding in a good, modern IDE, you're coding with one hand tied behind your back!
11 comments:
Why doesn't Set.contains() accept "? extends Foo" ?
public boolean contains(? extends E element) {
...
}
Just because the language doesn't do that sort of thing? Maybe it should?
been there... been stung by that. Yay for static analysis!
Kevin,
It's a really good post on a very important subject.
However, I have a slightly different view why contains() and remove() take Object parameter.
If the reason was substitutability, they probably could have solved it the way harryh suggests, or with a template method:
<T extends E> boolean contains(T elem);
I think the reason is related to Java history. Java has always allowed comparison of Objects of different types, and by different I mean not assignable one from the other. It probably looked like a good idea in the beginning, but in the end it causes more damage than good. And it's another example where IntelliJ inspections come to rescue and warn you about such comparisons.
Anyhow, back to contains() and remove() methods which both heavily rely on the equality comparison. Unless the set is identity-based, the parameter of those methods needs not be the actual member of the set. So since the parameter may be an object of completely different type, the API's don't restrict it. (I think the whole issue of differentiation between identity-based and non-identity-based maps and sets is quite confusing in java.util.*, I mean, look at WeakHashMap.)
Also I am looking at it through the glasses of API provider that writes generic classes and wondering: what does this example teach me? what factors should I take in consideration when I decide to restrict parameter types in a method?
P.S. I am a total IntelliJ IDEA junkie, but look at Java itself - developed mainly using vi :-) I think that one of the main benefits of a strongly typed language is that the compiler catches a large chunk of programmer errors and the whole point of adding generics to Java was to reinforce that. So I have mixed emotions regarding your closing sentence :-)
Nice post, Kevin...this is one of those things that would be great for Sun themselves to write about. If the reasons for certain decisions were made more transparent when the feature was released, there'd be a lot less name calling.
Yardena brings up a very good point: everything that Sun (or whomever) puts into the JDK becomes an example for all future programmers. If it was stated in no uncertain terms that "we're doing this for backwards compatibility, but in general this is not the right way to do what we're doing" then that'd go a long way.
I believe this is an erroneous statement:
"To preserve the substitutability principle, any method that wants to read from a set of Foos should be equally able to read from a set of SubFoos"
The Liskov Substituion Principle (and substitutability in general) refers to the substitution of supertypes with subtypes. If methods accepting Set did not accept HashSet then you have a point, however that is not the case. Set< Foo> is not a supertype of Set< SubFoo>. It is a parameterized variant, and therefore not substitutable.
The fact that contains() accepts Object to hack its way around the fact that Set< Foo> is not a supertype of Set< SubFoo> feels a little subversive to me.
Dhanji.
I think Set.contains() take an Object, not an E, since Object.equals take a Object not an E. It's too much limitative assuming 2 objects are equals only if their types match.
regards
" giovanni said...
I think Set.contains() take an Object, not an E, since Object.equals take a Object not an E. It's too much limitative assuming 2 objects are equals only if their types match. "
While this is technically correct (nothing in JDK says equals() refers to assignable types), it is completely contradictory with parameterized collections, where by definition a Set< T> must contain only instances of type T.
I agree with you Dhanji: it's a backward compatibility issue. However until Object.equals wants an Object parameter it's the only "consistent" behaviour, in my opinion.
I just checked C# and its generic collection interface known as ICollection< T > requires methods with these signatures to be implemented:
void Add(T item)
void Clear()
bool Contains(T item)
void CopyTo(T[] array, int arrayIndex)
int Count {get;}
bool IsReadOnly {get;}
bool Remove(T item)
so C# is defined exactly the way one would expect, with not an Object type in sight.
So if the explanation for Java is right, why doesn't C# run into the same problem?
Why was there no follow on bankruptcy then? The bailout of AIG FP went to (wow power leveling) hedge funds that bound credit swaps on Lehman failing or others betting on rating (wow power leveling) declines. AIG has drained over 100 billion from the government. Which had to go to (wow power leveling) those who bet on failures and downgrades. Many of whom (power leveling)were hedge funds. I-banks that had offsetting swaps needed the money from the AIG bailout or they would have been caught. Its an (wow powerleveling) insiders game and it takes just a little bit too much time for most people to think (wow gold) through where the AIG 100 billion bailout money went to, hedge funds and players, many of whom hire from the top ranks of DOJ, Fed, Treasury, etc. ZHANG XIAO CHEN
cheap wedding gowns,
discount bridal gowns,
China wedding dresses,
discount designer wedding dresses,
China wedding online store,
plus size wedding dresses,
cheap informal wedding dresses,
junior bridesmaid dresses,
cheap bridesmaid dresses,
maternity bridesmaid dresses,
discount flower girl gowns,
cheap prom dresses,
party dresses,
evening dresses,
mother of the bride dresses,
special occasion dresses,
cheap quinceanera dresses,
hot red wedding dresses
Post a Comment