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?
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.
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With