20

Is it a good practice to start a method name with "Does" (in C#)? It looks a little bit weird to me, so I would like to get your opinion.

I am writing a method which check if an account exists or not, should the signature be "bool DoesAccountExist(id)"? Is there a better name?

Thanks!

Martin
  • 39,309
  • 62
  • 192
  • 278
  • 2
    It looks like you are not thinking ahead to how your boolean returning function will be used. Go with AccountExists, I upvoted accordingly. – Jodrell May 04 '11 at 17:37
  • 9
    `bool answer = DoesAMethodNameLookGoodStartingWith("Does");` – BoltClock May 04 '11 at 18:17

7 Answers7

38

Personally I'd go with AccountExists(id) in this case, since it will look more natural in an if block

if (AccountExists(id)) { }
Patrick
  • 5,526
  • 14
  • 64
  • 101
23

We always use Is for any method that will return a boolean, so in this case we would call the method

IsExistingAccount(id)
Leons
  • 2,679
  • 1
  • 21
  • 25
  • 1
    amazingly but this helped my rectangular programmer head quite a bit :) the point being the 'existing' form, you can place everything under that - like `IsRefreshing...` - and I too like the idea of having a single prefix for all - otherwise you need 'should' 'has' 'does' etc. depending on what you're dealing w/ - the most helpful answer of all – NSGaga-mostly-inactive Apr 17 '16 at 17:10
4

I usually leave off the Does and simply use AccountExists(id).

To me it looks more natural later in code such as:

if(AccountExists(id))
{
    ValidateLogin();
}

The alternative would be something like:

if(!DoesAccountExist(id)) {}

seems odd to me. I would read that as if does account not exist? Which makes less then account does not exist.

stivlo
  • 83,644
  • 31
  • 142
  • 199
PFranchise
  • 6,642
  • 11
  • 56
  • 73
2

The "Is" prefix idiom help clarify methods like Array "Empty" which might mean "empty the array" or "is the array empty?". If used consistently "IsEmpty" is clearly the bool return and by convention "Empty" becomes clear as the action of emptying.
In the context of the question I agree with @Andy Whites second suggest:

if (IsExitingAccount) ...

Here the 'Is' triggers a strong automatic implication of bool return constistant with its use else where (IsEmpty, IsNull, etc)

Ricibob
  • 7,505
  • 5
  • 46
  • 65
1

I think AccountExists is your best bet, but one other option is: IsExistingAccount. For methods or properties that return a boolean, it can be useful to use a naming convention where the method begins with the word "is". If you use "is," you'll have to rephrase the rest of the method name so that it makes sense to the reader.

Andy White
  • 86,444
  • 48
  • 176
  • 211
0

Another Option is to use the standard C# plural of Is, Any.

AnyAccountsExist(id)
RitchieD
  • 1,831
  • 22
  • 21
0

Can or Is could be used like CanCopy or IsAccountExists is fine too. Following predefined conventions make code more readable. To be frank Does prefix looks a bit wierd to me as well.

FIre Panda
  • 6,537
  • 2
  • 25
  • 38