Boolean method naming readability

Naming ConventionsReadability

Naming Conventions Problem Overview


Simple question, from a readability standpoint, which method name do you prefer for a boolean method:

public boolean isUserExist(...)

or:

public boolean doesUserExist(...)

or:

public boolean userExists(...)

Naming Conventions Solutions


Solution 1 - Naming Conventions

public boolean userExists(...)

Would be my prefered. As it makes your conditional checks far more like natural english:

if userExists ...

But I guess there is no hard and fast rule - just be consistent

Solution 2 - Naming Conventions

I would say userExists, because 90% of the time my calling code will look like this:

if userExists(...) {
  ...
}

and it reads very literally in English.

if isUserExist and if doesUserExist seem redundant.

Solution 3 - Naming Conventions

Beware of sacrificing clarity whilst chasing readability.

Although if (user.ExistsInDatabase(db)) reads nicer than if (user.CheckExistsInDatabase(db)), consider the case of a class with a builder pattern, (or any class which you can set state on):

user.WithName("Mike").ExistsInDatabase(db).ExistsInDatabase(db2).Build();

It's not clear if ExistsInDatabase is checking whether it does exist, or setting the fact that it does exist. You wouldn't write if (user.Age()) or if (user.Name()) without any comparison value, so why is if (user.Exists()) a good idea purely because that property/function is of boolean type and you can rename the function/property to read more like natural english? Is it so bad to follow the same pattern we use for other types other than booleans?

With other types, an if statement compares the return value of a function to a value in code, so the code looks something like:

if (user.GetAge() >= 18) ...

Which reads as "if user dot get age is greater than or equal to 18..." true - it's not "natural english", but I would argue that object.verb never resembled natural english and this is simply a basic facet of modern programming (for many mainstream languages). Programmers generally don't have a problem understanding the above statement, so is the following any worse?

if (user.CheckExists() == true)

Which is normally shortened to

if (user.CheckExists())

Followed by the fatal step

if (user.Exists())

Whilst it has been said that "code is read 10x more often than written", it is also very important that bugs are easy to spot. Suppose you had a function called Exists() which causes the object to exist, and returns true/false based on success. You could easily see the code if (user.Exists()) and not spot the bug - the bug would be very much more obvious if the code read if (user.SetExists()) for example.

Additionally, user.Exists() could easily contain complex or inefficient code, round tripping to a database to check something. user.CheckExists() makes it clear that the function does something.

See also all the responses here: https://stackoverflow.com/questions/1370840/naming-conventions-what-to-name-a-method-that-returns-a-boolean

As a final note - following "Tell Don't Ask", a lot of the functions that return true/false disappear anyway, and instead of asking an object for its state, you tell it to do something, which it can do in different ways based on its state.

Solution 4 - Naming Conventions

The goal for readability should always be to write code the closest possible to natural language. So in this case, userExists seems the best choice. Using the prefix "is" may nonetheless be right in another situations, for example isProcessingComplete.

Solution 5 - Naming Conventions

I would go with userExists() because 1) it makes sense in natural language, and 2) it follows the conventions of the APIs I have seen.

To see if it make sense in natural language, read it out loud. "If user exists" sounds more like a valid English phrase than "if is user exists" or "if does user exist". "If the user exists" would be better, but "the" is probably superfluous in a method name.

To see whether a file exists in Java SE 6, you would use File.exists(). This looks like it will be the same in version 7. C# uses the same convention, as do Python and Ruby. Hopefully, this is a diverse enough collection to call this a language-agnostic answer. Generally, I would side with naming methods in keeping with your language's API.

Solution 6 - Naming Conventions

My simple rule to this question is this:

If the boolean method already HAS a verb, don't add one. Otherwise, consider it. Some examples:

$user->exists()
$user->loggedIn()
$user->isGuest() // "is" added

Solution 7 - Naming Conventions

There are things to consider that I think were missed by several other answers here

  1. It depends if this is a C++ class method or a C function. If this is a method then it will likely be called if (user.exists()) { ... } or if (user.isExisting()) { ... }
    not if (user_exists(&user)) . This is the reason behind coding standards that state bool methods should begin with a verb since they will read like a sentence when the object is in front of them.

  2. Unfortunately lots of old C functions return 0 for success and non-zero for failure so it can be difficult to determine the style being used unless you follow the all bool functions begin with verbs or always compare to true like so if (true == user_exists(&user))

Solution 8 - Naming Conventions

Purely subjective.

I prefer userExists(...) because then statements like this read better:

if ( userExists( ... ) )

or

while ( userExists( ... ) )

Solution 9 - Naming Conventions

In this particular case, the first example is such horrible English that it makes me wince.

I'd probably go for number three because of how it sounds when reading it in if statements. "If user exists" sounds better than "If does user exists".

This is assuming it's going to be to used in if statement tests of course...

Solution 10 - Naming Conventions

I like any of these:

userExists(...)
isUserNameTaken(...)
User.exists(...)
User.lookup(...) != null

Solution 11 - Naming Conventions

Method names serves for readability, only the ones fit into your whole code would be the best which most of the case it begins with conditions thus subjectPredicate follows natural sentence structure.

Solution 12 - Naming Conventions

Why not rename the property then?

if (user.isPresent()) {

Solution 13 - Naming Conventions

Since I follow the convention to put verb before function name, I would do the same here too:

//method name
public boolean doesExists(...)

//this way you can also keep a variable to store the result
bool userExists = user.doesExists()

//and use it like a english phrase
if (userExists) {...}

//or you can use the method name directly also and it will make sense here too
if (user.doesExists()) {...}

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
QuestionYuval AdamView Question on Stackoverflow
Solution 1 - Naming ConventionsMartinView Answer on Stackoverflow
Solution 2 - Naming ConventionsKaiView Answer on Stackoverflow
Solution 3 - Naming ConventionsMichael ParkerView Answer on Stackoverflow
Solution 4 - Naming ConventionsKonamimanView Answer on Stackoverflow
Solution 5 - Naming ConventionsDavidView Answer on Stackoverflow
Solution 6 - Naming ConventionsJonathanView Answer on Stackoverflow
Solution 7 - Naming ConventionsLee BallardView Answer on Stackoverflow
Solution 8 - Naming ConventionszumalifeguardView Answer on Stackoverflow
Solution 9 - Naming ConventionsDanaView Answer on Stackoverflow
Solution 10 - Naming ConventionsJohn KugelmanView Answer on Stackoverflow
Solution 11 - Naming ConventionsYuanView Answer on Stackoverflow
Solution 12 - Naming ConventionsArtem LukaninView Answer on Stackoverflow
Solution 13 - Naming ConventionsmadhurgargView Answer on Stackoverflow