Java : naming convention for accessors

末鹿安然 提交于 2019-12-23 10:17:21

问题


I'm looking for the official naming convention in Java regarding accessors.

I've seen that, for instance, the JPanel class deprecated the size() method in favor of getSize().

But in the ArrayList class, the method is size().

So I'm wondering if accessors should be named getXXX() or xXX() ?


回答1:


It's usually a bad idea to not use the JavaBeans convention (getters and setters).
They're used through reflection by many frameworks, in particular with EL where sometimes you can't access your fields without the rights getters (depending on the EL flavour).

So your accessors should always be named getXxx() or isXxx() and setXxx().

size() in the collection framework is an example of "flaw" which can annoy developers (see link below). The choice made by Josh Bloch and Neal Gafter to make it more readable now makes it difficult to obtain in some contexts (EL).

But remember the JavaBeans convention isn't the Java naming convention.


Resources :

  • How to Access the Size of a Collection in a JSP Page Using JSTL-EL
  • Java conventions
  • JavaBeans
  • JavaBeans specification

On the same topic :

  • Getters on an immutable type



回答2:


With query methods, I always look at getXXX as something that is provided versus something that is calculated. The size() method returns the size of the collection which is a derived value, so it makes sense. If you had getSize() my assumption would be that I could somehow set the size (through a constructor or setter method).




回答3:


For anything trying to look like a JavaBean, it should be getXXX or isXXX. (I can't remember whether hasXXX is valid for Boolean properties as well... not sure.)

It makes sense to treat a JPanel in a bean kind of way - for designers etc - but not an ArrayList.

Personally I tend to use the getXXX form just for consistency, but I believe the above is the reasoning involved in ArrayList's naming.




回答4:


This is only a formative addentum to Colin HERBERT's answer which, in my opinion, is enough:

  • Accessor method signatures should always look like public Type getProperty(). Additionally, accessors should always return a copy of the property's value, not the value itself.
  • Mutator method signatures should always look like public void setProperty(Type value)

Combining an accessor and a mutator gives you a JavaBean property. JavaBeans are not considered to be immutable by nature, but if you want to make it immutable, you should use the following signature for the mutator method: public YourJavaBean withProperty(Type value). Note that this should always return a completely new YourJavaBean instance with copied property values.




回答5:


In Eclipse the convention is definitely to use the getpattern. The automisation tools create and handle getters by checking for and writing get and set style accessors.




回答6:


I prefer the get/is/set-Convention (especially for ValueObjects/DataObjects) not only because it's the JavaBeans specification but because of the following two points.

  1. The clear prefix of the method seperates property related methods from 'logic' methods.
  2. You can use it out of the box with JSP and other frameworks that assume this naming.



回答7:


It is always better to follow setXXX and getXXX pattern as per JavaBeans specification. The size() method named so, may be as it is just querying the state.



来源:https://stackoverflow.com/questions/3698171/java-naming-convention-for-accessors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!