Alex Rivera | Logout

`name()` vs. `getName()` naming convention?

Asked 2012-08-29T23:41:07.973
8

I have seen some projects (jsoup for example) are using this new getter/setter naming convention:

String name()
void   name(String value)

instead of old getter/setter convetion:

String getName()
void   setName(String value)

What are positive and negative aspects of each naming convention? When and why you think one should be preferred over the other?

Edit
Report

1 Answer

9

The first example doesn't adhere to the JavaBeans specification [Warning PDF]. There are certain frameworks like Spring, that assume this naming convention, especially when you do something in EL like ${object.name}, which gets translated to object.getName(). This will fail if you don't follow the naming conventions (although there are ways to get around it).

Without getting into a discussion about when/if to use getters/setters, in general it's better to stick with the naming convention because there are fewer surprises that way, especially when you're integrating with third-party libraries or frameworks that expect things to be named according to convention.

answered 2012-08-29T23:44:17.300

Your Answer