views:

194

answers:

5

I like using question mark at the end of method/function names in other languages. Java doesn't let me do this. As a work around, how else can I name boolean returning methods in java ? Using an 'is', 'has', 'should','can' in the front of method sound ok for some cases. Is there a better way to name such methods ?

For e.g. createFreshSnapshot?

+11  A: 

The convention is to ask a question in the name.

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.

If you do a Google Search for isEmpty() in the Java API, you get lots of results.

jjnguy
`public boolean isTheCollectionEmpty();` `public boolean doesThisNodeHaveChildren();` or how about `public boolean isThisFunctionNameVerbose();`
Erick Robertson
what about createFreshSnapshot?
letronje
@letr, well, I would probably rename it lit `isSnapshotExpired` or something like that. (based on your criteria)
jjnguy
@Erick, I don't understand.
jjnguy
@Justin Could you add the 'isSnapshotExpired' name to your main answer ?
letronje
@Letro, I will add some more info.
jjnguy
@Letro, added. Let me know what ya think.
jjnguy
I get round the 'createFrshSnapshot' issue by using the word 'should' - i.e. "shouldCreateFreshSnapshot()" (although in this case isSnapshotExpired() is better)
DJClayworth
I check to see if heartbeat messages should be sent with a `shouldHeartbeat()` method.
Erick Robertson
+3  A: 

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

BillThor
A: 

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
+5  A: 

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);
Jason S
It's confusing when you say `public boolean is<PropertyName>();` It looks like a generic.
Erick Robertson
Hey, I'm just quoting the spec. I'll italicize as per the spec.
Jason S
I don't care abt Java beans compatibility, I just want my method names to sound right :)
letronje
to whoever "-1"d this, could you explain why? It's a legitimate issue not covered by other answers here, and my answer started with "If". If you don't wish your class to be compatible with the Java Beans spec, do what you like.
Jason S
+1  A: 

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
But try doesn't convey that it's a question (has a return value).
Steve Kuo