Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the FDG for naming boolean properties?

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:

  • Enable
  • Enabled
  • IsEnabled
  • Disable
  • Disabled
  • IsDisabled

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 ?

like image 845
Max Toro Avatar asked Oct 12 '09 18:10

Max Toro


People also ask

How to name boolean properties?

✔️ 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.

How do you name Booleans in Python?

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.

How to name boolean variable in c++?

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.

How to name properties c#?

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.


2 Answers

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.

like image 155
adrianbanks Avatar answered Sep 23 '22 05:09

adrianbanks


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.

like image 26
Grumdrig Avatar answered Sep 19 '22 05:09

Grumdrig