Stop Exposing Collections Already(davybrion.com)
davybrion.com
Stop Exposing Collections Already
http://davybrion.com/blog/2009/10/stop-exposing-collections-already/
1 comments
I second that. If you are using Java, simply return Collections.unmodifiableList(listObject);
Thanks for that. I was going to comment that the article carries a very complaining tone without giving any real solutions.
This requires BOTH the API maintainer AND the caller to add code, all in the name of avoiding some theoretical violation of encapsulation. The extra code risks bugs, is harder to maintain, harder to read, harder to understand, and may accidentally limit the usefulness of the return value in some critical way (which would make the API temporarily unusable while some bug is opened to have the API expanded).
There's a lot of things they could try first, including simply adding documentation to tell the caller what must not happen to the collection that is returned.