why .size() is not getSize() on collections

api, collections, java

Solution

Verbatim quote from the "Java Collections API Design FAQ"

Why didn't you use "Beans-style names" for consistency?

While the names of the new collections methods do not adhere to the "Beans naming conventions", we believe that they are reasonable, consistent and appropriate to their purpose. It should be remembered that the Beans naming conventions do not apply to the JDK as a whole; the AWT did adopt these conventions, but that decision was somewhat controversial. We suspect that the collections APIs will be used quite pervasively, often with multiple method calls on a single line of code, so it is important that the names be short. Consider, for example, the Iterator methods. Currently, a loop over a collection looks like this:

`for (Iterator i = c.iterator(); i.hasNext(); ) System.out.println(i.next());`

Everything fits neatly on one line, even if the Collection name is a long expression. If we named the methods "getIterator", "hasNextElement" and "getNextElement", this would no longer be the case. Thus, we adopted the "traditional" JDK style rather than the Beans style.

Problem

I have been using Java for quite some time and am still puzzled by this naming of API. Why is it like that? For example when you have one instance of `ArrayList` it has a property of how much objects are added to the list. To get this property you call `.size()` and not `getSize()` which would be more along the line of what you are actually trying to do. You are trying to read a value of the property of this list, not do some operation on it. I guess internally "sizing" this list is probably much more complicated then just reading a single variable but that is implantation detail and the user of the object should not care about that.

Original source

Related problems