What is the Framework Design Guideline for naming boolean properties? If there isn't one, then what's your recommendation?
Let's say I have a User
class, and I need a property that specifies if the user is enabled or not. These are the options I can think of:
Also, if the BL says that the user must be disabled by default and explicitly enabled, should I prefer an 'enable' variation, considering that the default value for System.Boolean
is false
?
✔️ DO name Boolean properties with an affirmative phrase ( CanSeek instead of CantSeek ). Optionally, you can also prefix Boolean properties with "Is", "Can", or "Has", but only where it adds value. ✔️ CONSIDER giving a property the same name as its type.
Function names should be lowercase, with words separated by underscores as necessary to improve readability. Typically, people start a function with is (e.g. is_palindrome ) to describe that a boolean value is returned.
When naming booleans, you should avoid choosing variable names that include negations. It's better to name the variable without the negation and flip the value. If you absolutely can't (see Guideline 2 below), then try to find an already-negated form of the concept you are trying to express.
Always use camelCase with method arguments and local variables. Don't use Hungarian notation for variables. Note: Don't use abbreviations for any words and don't use underscore ( _ ) in between any name. Use PascalCase for property.
The Framework Design Guidelines (Brad Abrahms and Krzysztof Cwalina) say to use either Enabled
or IsEnabled
(section 3.6.2). They say to use affirmative phrases (ie. CanSeek instead of CantSeek), and to use the most readable version (ie. Created is more readable than IsCreated).
I would personally use Enabled
in your case, with a default value of false
. User.Enabled
reads well and is clear in what its meaning is.
I'd avoid the "disabled" variation, since a double negative like "disabled = false" is much harder to comprehend than "enabled = true".
I'd also prefer an adjectival form for a property to the verb "enable", which would be a better method name.
That narrows it down to "enabled" or "isEnabled", which is probably a matter of personal style & convention. The latter emphasizes that it's a bool; the former is more succinct.
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