I have always found it very useful to adhere to the Java Beans naming conventions: getX()
, setX()
, isX()
, etc. I think the Java Bean naming conventions provide several main advantages:
- When looking through code, you can immediately determine the purpose of a method.
- When looking through the API documentation, all methods are grouped together due to the alphabetical ordering of the Java documentation.
- When using the code completion feature of any IDE, it is easy and intuitive to sort through and find the method that you are looking for. This is again due to the alphabetical ordering of the IDE.
I know that there are many classes that are part of the Java API that do not use the Java Beans naming conventions. An example would be ArrayList
, which has methods such as size()
.
I actually have two main questions:
- Is it a good idea to always try to use the Java Beans naming conventions? If not, why?
- I know that
ArrayList
was written before Java Beans came out. Is it customary for Oracle to use the Java Beans naming conventions for all (or at least most) new content?
The below is more to get people to think rather than an actual question.
As I stated above, I have always stuck with the Java Bean naming conventions. I think they provide a lot of advantages in a lot of ways. I think the biggest problem is that without Java Beans, a lot of people use confusing naming conventions
I personally believe that the following should always be adhered to, and I'm curious to see what people think about it:
- If the method name is a noun, the method is expected to return a value.
- If the method name is a verb, the method should always do something. If the method returns a value, the returned value should be related to the action that was performed.
- If the method name is an adjective, the name should not be used alone. For example,
big()
should beisBig()
. - Nouns that are not an implicit property of an object should not be used alone.
For example:size()
is an appropriate method of aHouse
object, butdoor()
is not. The reason for this is thatsize
is an inherit property of ahouse
that was decided by nature. It is not physically required for ahouse
to have adoor
.
Instead ofdoor()
, the method should be namedgetDoor()
. - If the method name adheres to multiple parts of speech, the method is always going to require unnecessary thinking from the users end. Because of this, the method should probably be rename.
For example:
Let's say I have a method calledprofile()
. Depending on the class it is part of,profile()
might be a verb or a noun. I could be profiling something, or I could be getting the profile. Changing the name of the method togetProfile()
ordoProfiling()
would add clarification.