Naming conventions for Java methods that return boolean
I like using question mark at the end of method/function names in other languages. Java doesn't let me do this. As a workaround how else can I name boolean returning methods in Java? Using an is
, has
, should
, can
in the front of a method sound okay for some cases. Is there a better way to na开发者_C百科me such methods?
For e.g. createFreshSnapshot?
The convention is to ask a question in the name.
Here are a few examples that can be found in the JDK:
isEmpty()
hasChildren()
That way, the names are read like they would have a question mark on the end.
Is the Collection empty?
Does this Node have children?
And, then, true
means yes, and false
means no.
Or, you could read it like an assertion:
The Collection is empty.
The node has children
Note:
Sometimes you may want to name a method something like createFreshSnapshot?
. Without the question mark, the name implies that the method should be creating a snapshot, instead of checking to see if one is required.
In this case you should rethink what you are actually asking. Something like isSnapshotExpired
is a much better name, and conveys what the method will tell you when it is called. Following a pattern like this can also help keep more of your functions pure and without side effects.
If you do a Google Search for isEmpty()
in the Java API, you get lots of results.
If you wish your class to be compatible with the Java Beans specification, so that tools utilizing reflection (e.g. JavaBuilders, JGoodies Binding) can recognize boolean getters, either use getXXXX()
or isXXXX()
as a method name. From the Java Beans spec:
8.3.2 Boolean properties
In addition, for boolean properties, we allow a getter method to match the pattern:
public boolean is
<PropertyName>()
;This “is<PropertyName>” method may be provided instead of a “get<PropertyName>” method, or it may be provided in addition to a “get<PropertyName>” method. In either case, if the “is<PropertyName>” method is present for a boolean property then we will use the “is<PropertyName>” method to read the property value. An example boolean property might be:
public boolean isMarsupial(); public void setMarsupial(boolean m);
I want to post this link as it may help further for peeps checking this answer and looking for more java style convention
Java Programming Style Guidelines
Item "2.13 is prefix should be used for boolean variables and methods." is specifically relevant and suggests the is prefix.
The style guide goes on to suggest:
There are a few alternatives to the is prefix that fits better in some situations. These are has, can and should prefixes:
boolean hasLicense();
boolean canEvaluate();
boolean shouldAbort = false;
If you follow the Guidelines I believe the appropriate method would be named:
shouldCreateFreshSnapshot()
For methods which may fail, that is you specify boolean as return type, I would use the prefix try
:
if (tryCreateFreshSnapshot())
{
// ...
}
For all other cases use prefixes like is..
has..
was..
can..
allows..
..
Standard is use is
or has
as a prefix. For example isValid
, hasChildren
.
is
is the one I've come across more than any other. Whatever makes sense in the current situation is the best option though.
I want to point a different view on this general naming convention, e.g.:
see java.util.Set: boolean add(E e)
where the rationale is:
do some processing then report whether it succeeded or not.
While the return
is indeed a boolean
the method's name should point the processing to complete instead of the result type (boolean for this example).
Your createFreshSnapshot
example seems for me more related to this point of view because seems to mean this: create a fresh-snapshot then report whether the create-operation succeeded. Considering this reasoning the name createFreshSnapshot
seems to be the best one for your situation.
精彩评论