183

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(...)
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395

13 Answers13

163
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

Martin
  • 39,569
  • 20
  • 99
  • 130
  • 3
    "makes your {method call} far more like natural english" sounds like a great test for rational naming across the board. clarified my thinking on the matter - thanks! – cori Jun 02 '11 at 12:06
  • 37
    On the other hand, in isolation or when not immediately following "if", then "userExists()" sounds like a statement of fact, rather than the question it was intended as. Unlike "IsUserExisting()" or "DoesUserExist()", which follows English natural language word order rules for straight-forward questions. – Oskar Berggren Aug 15 '16 at 14:49
  • 9
    ..but why would methods returning a bool be used outside an `if`? If they have side-effects that's even more of a smell. `if IsUserExisting()` and `if DoesUserExist()` looks horrendous and should be avoided. – RJFalconer Jul 20 '18 at 09:11
  • 7
    @RJFalconer sometimes you may need to use result of that method in several places, so you will assign it to variable. Since method is called `userExists`, what name of variable you will declare? `userExists` is good for variables, not methods. As @Oskar wrote - it sound like statement, not a question. – Jarosław Wlazło Feb 13 '19 at 22:25
  • For situations where there has to a be subject, predicate, and an object, for example, UserSessionIsComplete or IsUserSessionComplete, which one do you prefer? – Yang Jun 19 '19 at 19:53
  • @Yang I prefer consistency every time. – Martin Sep 28 '19 at 13:57
  • what if i need a method to check if a UFO exists? if UFOExists() ? It looks bad if i need to start with capital case – user1955934 Jul 17 '20 at 12:16
  • Then follow rule 2 - be consistent – Martin Jul 24 '20 at 14:19
  • It feels like `userExists` would be the boolean that I would assign it to, e.g. `userExists = getUserExistence()` – ajHurliman Jun 24 '21 at 18:30
  • @RJFalconer for example, the method of a controller in an API, or the use case to which this controller will call. Neither of those will be used inside if statements. – Javi Marzán Oct 20 '21 at 15:02
52

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.

Sam R.
  • 16,027
  • 12
  • 69
  • 122
Kai
  • 9,444
  • 6
  • 46
  • 61
30

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

Community
  • 1
  • 1
Michael Parker
  • 7,180
  • 7
  • 30
  • 39
  • 9
    > `Suppose you had a function called Exists() which causes the object to exist` That is already a problem. Such a method should be a verb, like `Create`. At the very least it would be `Exist`, but "exist" as a verb is rarely used. `It's not clear if ExistsInDatabase is checking whether it does exist, or setting the fact that it does exist.` It's very clear. I would assert that most developers would be surprised if that did anything other than just return a boolean. – RJFalconer Jul 20 '18 at 09:19
  • 3
    @RJFalconer `Most developers` is the key to your sentence there. I would say `all developers` would be surprised if `CheckExists()` does anything other than check something exists. It's not that `Exists()` is a terrible name, just that `CheckExists()` is a _better_ name, and this question is asking, as a general principle, whats the best naming pattern? The answer is to treat it like any other function, start the name with a verb, and dont use a different pattern just because it returns a boolean. – Michael Parker Jul 22 '18 at 12:02
  • 1
    Yes, the question is about the best naming pattern BUT for the boolean methods. Bool methods are unique and have their own common name - predicate. You shouldn't treat them like other functions. Putting a verb alongside the question in boolean method name is redundant. And it has a negative impact on code readability. Naming boolean methods in form of questions, without any verbs is accepted as the best practice in the industry. Examples: https://learn.microsoft.com/en-us/dotnet/api/system.io.file.exists https://developer.android.com/reference/java/io/File#exists() – Almir Feb 07 '19 at 11:10
  • 1
    @Almir File.Exists is an extremely old call (at least dot net 1.1) and is not a good example of modern readability standards. Look at the modern dot net core API for more modern examples of how Microsoft agrees: https://github.com/dotnet/sdk, some random examples [link](https://github.com/dotnet/sdk/blob/master/src/Cli/Microsoft.DotNet.Cli.Utils/ArgumentEscaper.cs#L187) [link](https://github.com/dotnet/sdk/blob/master/src/Cli/Microsoft.DotNet.Cli.Utils/DangerousFileDetector.cs#L12) [link](https://github.com/dotnet/sdk/blob/master/src/Cli/Microsoft.DotNet.Cli.Utils/CommandContext.cs#L20-L28) – Michael Parker May 05 '20 at 13:50
16

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.

Konamiman
  • 49,681
  • 17
  • 108
  • 138
13

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
Jonathan
  • 18,229
  • 10
  • 57
  • 56
9

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.

Marcel Bro
  • 4,907
  • 4
  • 43
  • 70
David
  • 1,187
  • 1
  • 10
  • 22
5

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

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
Lee Ballard
  • 1,049
  • 11
  • 13
4

Why not rename the property then?

if (user.isPresent()) {
Artem Lukanin
  • 556
  • 3
  • 15
2

Purely subjective.

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

if ( userExists( ... ) )

or

while ( userExists( ... ) )
AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
zumalifeguard
  • 8,648
  • 5
  • 43
  • 56
1

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

Dana
  • 32,083
  • 17
  • 62
  • 73
1

I like any of these:

userExists(...)
isUserNameTaken(...)
User.exists(...)
User.lookup(...) != null
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

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.

Yuan
  • 2,690
  • 4
  • 26
  • 38
0

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()) {...}
madhurgarg
  • 1,301
  • 2
  • 11
  • 17