Naming conventions for java methods returning boolean values ​​(no question mark)

I like to use the question mark at the end of method / function names in other languages. Java does not allow me to do this. How to work, how else can I name the logical return methods in java? In some cases, using "is", "has", "should", "can" at the beginning of the method sounds normal. Is there a better way to name such methods?

For example, createFreshSnapshot?

+58
java methods naming-conventions
Oct 06 2018-10-10
source share
6 answers

The agreement is to ask a question in the title.

isEmpty() hasChildren() 

Thus, the names are read as if they will have a question mark at the end.

Is the collection empty?
Does this Node have kids?

And then true means yes, and false means no.

Or you can read this as a statement:

The collection is empty.
Node has children

Note:
Sometimes you may need to name a method similar to createFreshSnapshot? . Without a question mark, the name implies that the method should create a snapshot, not check if it is required.

In this case, you must 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 use Google Search for isEmpty() in the Java API, you get a lot of results.

+75
Oct 06 '10 at 15:50
source share

If you want your class to be compatible with the Java Beans specification so that tools using reflection (e.g. JavaBuilders , JGoodies Binding ) can recognize logical getters, either use getXXXX() or isXXXX() as the method name. From the Java Beans specification:

8.3.2 Boolean properties

In addition, for Boolean properties, we allow the getter method to match the pattern:

public boolean is <PropertyName> () ;

This "is <PropertyName>" method may be provided in place of the "get <PropertyName>" method or may be provided in addition to the "get <PropertyName>" method. In any case, if the "is <PropertyName>" method is present for a boolean property, we will use the "is <PropertyName>" method to read the value of the property. An example of a logical property might be:

 public boolean isMarsupial(); public void setMarsupial(boolean m); 
+22
06 Oct 2018-10-10
source share

I want to post this link as it may help to further view this answer and find a more conditional agreement on java

Java Style Guides

Paragraph 14 is of particular importance and offers a prefix.

The style guide further suggests:

There are several alternatives to the prefix that works best in some situations. They have, can and should have prefixes:

 boolean hasLicense(); boolean canEvaluate(); boolean shouldAbort = false; 

If you follow the Guide, I believe that the appropriate method will be called:

 shouldCreateFreshSnapshot() 
+14
Apr 04 2018-12-12T00:
source share

For methods that may not work, you specify boolean as the return type, I would use the try prefix:

 if (tryCreateFreshSnapshot()) { // ... } 

For all other cases, use prefixes of the type is.. has.. was.. can.. allows.. ..

+9
Oct 06 2018-10-06
source share

The standard is to use 'is' or 'has' as a prefix. For example isValid , hasChildren .

+3
Oct 06 '10 at 15:50
source share

is is the one I came across more than any other. However, what makes sense in the current situation is the best option.

0
Oct. 06 '10 at 15:50
source share



All Articles