views:

86

answers:

2

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 ?

+4  A: 

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.

adrianbanks
It's interesting how some code from Microsoft does not follow this guideline, e.g.System.Web.Mvc.MvcHandler has a static property DisableMvcResponseHeader. Why did they choose to use Disable? is it because it's enabled by default and bool is false by default, therefore it's NOT disabled by default?
Max Toro
+2  A: 

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.

Grumdrig