3 ms·
> The only truly unforgiveable one is UnsupportedOperationException. The guy who wrote the collections API for Java didn't know the first thing about the Liskov
by ben509 7y ago
> The only truly unforgiveable one is UnsupportedOperationException. The guy who wrote the collections API for Java didn't know the first thing about the Liskov Substitution Principle, and gave the world an implementation that violates it...
The implementation perfectly follows it because the LSP simply requires identical behavior, and the contract clearly states it may[1] throw an exception. All the implementations "may" throw an exception. Sorry, but they correctly lawyered the LSP.
> I came up with around 20% more than the selected design.
But `add` can throw 5 different exceptions depending on restrictions your implementation wants to place on a given collection, does it allow for all that? In terms of making an interface that was small and reusable by many projects, runtime exceptions are a pretty good compromise for a standard library.
[1]: https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html#add(E) https://docs.oracle.com/javase/7/docs/api/java/util/Collecti...
- hinkley 7y agoNo they most definitely are not. One implementation receives a message. The other categorically rejects it. I cannot substitute them.
- ben509 7y agoYes, those are the same behavior. Both of them "may" accept it and "may" reject it, and "may" includes both "always" and "never". It is correct to the letter of LSP, but not the spirit of it, and this was a conscious decision; contrary to your original claim, they understood the principle perfectly well.