views:

234

answers:

8

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(...)
+7  A: 
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
For what it's worth, I agree with Martin.
Eric Palakovich Carr
+5  A: 

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.

Kai
+1 for readability
AJ
A: 

Purely subjective.

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

if ( userExists(...) )

or

while ( userExists(...) )

zumalifeguard
A: 

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

I like any of these:

userExists(...)
isUserNameTaken(...)
User.exists(...)
User.lookup(...) != null
John Kugelman
+2  A: 

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

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.

David
+2  A: 

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