Why Java is Not My Favorite Language — Reason #16.

UnsupportedOperationException. Why, in the name of all that is good and right, would you have such a dumb exception? Either the operation is important or it is not. If it is not don’t clutter the interface with it. If it is it ought to actually be implemented.

I have known about UnsupportedOperationException for a while now but I had not run into a method that actually threw it until yesterday. That method was ArrayList.remove(). I can just hear the lame argument for having this method throw UnsupportedOperationException now, “Removing an item from the underlying array would inefficient and/or hard to write.” If you feel any of that matters fine, but don’t lie to me and tell you have a bunch of operations, by claiming you implement the List interface, and then blow up in my face when I try to use one of those operation.

If remove() is really not a fundamental operation for lists (which I would debate, but whatever) it should not be part of List. There should be a RemoveList, or some such, interface which implements List and adds remove(). Interfaces are free, you can have as many of them as you need.

If you do not actually implement all methods of an interface you have no right to claim you implement the interface, regardless of what the javadoc comments say.

{Update: Curt Cox pointed out that I was, in fact, not using the normal ArrayList class. The offending class was actual java.util.Arrays$ArrayList which I can only assume, since there are no Javadocs for it, is an intentionally broken version of ArrayList.}

Comments 12