Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 753
Ali Shakiba Avatar asked Aug 29 '12 23:08

Ali Shakiba


2 Answers

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.

like image 147
Vivin Paliath Avatar answered Oct 15 '22 01:10

Vivin Paliath


Nothing except readability and API style.

Some APIs accept this style and some don't, like Spring injections and JSF can't recognize this syntax (they explicitly require get/set for properties).

like image 24
kosa Avatar answered Oct 15 '22 01:10

kosa