114

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?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
letronje
  • 9,002
  • 9
  • 45
  • 53

7 Answers7

147

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.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • what about createFreshSnapshot? – letronje Oct 06 '10 at 15:57
  • 4
    @letr, well, I would probably rename it lit `isSnapshotExpired` or something like that. (based on your criteria) – jjnguy Oct 06 '10 at 15:59
  • 9
    I get round the 'createFrshSnapshot' issue by using the word 'should' - i.e. "shouldCreateFreshSnapshot()" (although in this case isSnapshotExpired() is better) – DJClayworth Oct 06 '10 at 17:28
  • 1
    I check to see if heartbeat messages should be sent with a `shouldHeartbeat()` method. – Erick Robertson Oct 06 '10 at 17:41
  • Why think that `isEmpty()` and `hasChildren()` are *questions*, and not *assertions*? It actually reads slightly better in English if you think of those names as assertions or predicates. – rick Dec 19 '13 at 11:08
  • 1
    Probably for assertions it is better to name as `assertIsEmpty()` and `assertHasChildren()` – Steven Pribilinskiy Feb 17 '15 at 09:07
30

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);
Community
  • 1
  • 1
Jason S
  • 184,598
  • 164
  • 608
  • 970
  • It's confusing when you say `public boolean is();` It looks like a generic. – Erick Robertson Oct 06 '10 at 15:58
  • Hey, I'm just quoting the spec. I'll italicize as per the spec. – Jason S Oct 06 '10 at 15:59
  • 7
    I don't care abt Java beans compatibility, I just want my method names to sound right :) – letronje Oct 06 '10 at 15:59
  • 4
    Even if you don't care about Javabeans compatibility the naming convention is worth following, because it has grown way beyond the origina scope of 'beans' being configured in IDE's. For example if you use isEmpty as method name, you can call this method from JSP using object.empty, however you cannot call methods with other prefixes, so you cannot use object.children to call object.hasChildren(). So JSP and EL (Expression Language) lets you access Javabeans Properties. A big win if you ask me. – Stijn de Witt Dec 06 '10 at 14:00
  • JavaBeans is very specific, thanks for reminding that. I often wonder and tend to think I am stuck with the 'is' prefix, but not at all indeed. – Snicolas May 07 '15 at 20:20
29

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()
iknow
  • 8,358
  • 12
  • 41
  • 68
kommradHomer
  • 4,127
  • 5
  • 51
  • 68
  • 2
    FYI, there guidelines are copyright Geotechnical Software Services. That said, they have referenced source at the bottom to suggest they're legit. – Donal Lafferty Jun 26 '15 at 12:57
11

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

codymanix
  • 28,510
  • 21
  • 92
  • 151
  • 16
    But try doesn't convey that it's a question (has a return value). – Steve Kuo Oct 06 '10 at 16:25
  • 2
    Never return a boolean to indicate whether a method failed or not. If the method failed, throw an exception. Imagine the method failed and you get a false, how would you determine what exactly went wrong? – Wouter van Koppen Oct 29 '19 at 13:27
  • @WoutervanKoppen Agreed. It should be kept in mind that exceptions should be used for exceptional behaviour only. If it is common in the example above to not be able to create the fresh snapshot, then I would suggest creating a method `canCreateFreshSnapshost()` and only if the call succeeds I would call a method called `createFreshSnapshot`, which may return an exception if it unexpectedly fails. – Ladislav Ondris Oct 06 '22 at 09:03
5

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

Yves M.
  • 29,855
  • 23
  • 108
  • 144
BillThor
  • 7,306
  • 1
  • 26
  • 19
2

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

Skilldrick
  • 69,215
  • 34
  • 177
  • 229
1

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.

Adrian
  • 3,321
  • 2
  • 29
  • 46
  • 1
    I'm not sure why you are being downvoted, i agree. Even though i avoid using it, because it is not semantic at all, it is a convention used by many internal java API's. – Kamil Bęben Aug 19 '20 at 10:32
  • 1
    I guess because initially I only wrote "Set: boolean add​(E e)" so people didn't notice that is about the well known java.util.Set; or maybe because my English is so bad :D. Thanks for the upvote :) – Adrian Aug 19 '20 at 10:55