Naming conventions for Java methods that return boolean

JavaMethodsNaming Conventions

Java Problem Overview


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 name such methods?

For e.g. createFreshSnapshot?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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);

Solution 3 - Java

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()

Solution 4 - Java

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.. ..

Solution 5 - Java

Standard is use is or has as a prefix. For example isValid, hasChildren.

Solution 6 - Java

is is the one I've come across more than any other. Whatever makes sense in the current situation is the best option though.

Solution 7 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionletronjeView Question on Stackoverflow
Solution 1 - JavajjnguyView Answer on Stackoverflow
Solution 2 - JavaJason SView Answer on Stackoverflow
Solution 3 - JavakommradHomerView Answer on Stackoverflow
Solution 4 - JavacodymanixView Answer on Stackoverflow
Solution 5 - JavaBillThorView Answer on Stackoverflow
Solution 6 - JavaSkilldrickView Answer on Stackoverflow
Solution 7 - JavaAdrianView Answer on Stackoverflow