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?
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With