In Java you could return a list that cannot be modified by the caller by doing return Collections.unmodifiableList(list)
.
Should the method name indicate that it's returning an unmodifiable list? That could prevent clients of my class from attempting to add or remove objects from the list.
Best Answer
Not knowing that the method simply returns a
List
, my first reaction was this:Now, having learned what
Collections.unmodifiableList(list)
actually does, I have to say, yes, make it really obvious in the method name and in the object name.But most of all, I would try to avoid using this method at all. I would consider it very dangerous that the check is only performed at runtime.