Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Coding Standards Using a prefix "Is" or "Has" on Method Names

Is it advisable to prefix an "Is" or a "Has" when creating a method that returns a Boolean. My feeling is that this practice is more suited to defining property names.

Say, we have a method like the following has some logic:

bool IsActivePage()   {    // Some logic to determine if the page is active...    } 

Would it be more preferable to rename the method to GetActivePageStatus and then create a boolean property IsActivePage that returns the result of that method.

What is the .NET standard? All opinions will be appreciated?

like image 360
fin Avatar asked Jul 06 '11 14:07

fin


People also ask

Can method name start with is?

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.

How do you name a method in C#?

In C# the method name must start with uppercase letter and should be made of a verb or a couple: verb + noun. The name is formatted by following the Upper Camel Case convention (PascalCase), i.e. each word, including the first one, starts with uppercase. The brackets ( and ) always follow the name (without spaces).

What is Camelcase in C#?

In camel casing, two or more words are placed together without any space or underscore ( _ ), but the first letter of the first word is in lowercase and the first letter of the next word is capitalized.


2 Answers

The Framework Design Guidelines state that you should "give methods names that are verbs or verb phrases" since "typically methods act on data". Properties, on the other hand, should be named "using a noun, noun phrase, or an adjective" and "you can also prefix Boolean properties with Is, Can, or Has, but only where it adds value".

In this case, you are using a method rather than a property, probably since it is either expensive or has some side effects. I suggest you choose the name that provides the most clarity of what the returned value represents. The important part is that you're being consistent and that you're not confusing other developers with your convention.

like image 128
Anders Fjeldstad Avatar answered Sep 24 '22 14:09

Anders Fjeldstad


I would be using

bool IsActivePage {   get   {     // some logic   } } 

if the method has no side effects and is inexpensive.

I see no need to have both a method and a property for the same thing.

like image 38
Ian Ringrose Avatar answered Sep 20 '22 14:09

Ian Ringrose