13

someone wants me to make other people's code compliant to some FxCop ruleset which includes rule CA1726:Use preferred terms. Most of the terms/replacements are all right and I can understand that one has to decide on one single way to name things.

However, what's the deal with the term 'flags'? Can anyone explain to me why I shall not use this name? (before I go and complain about it at my boss ;) )

Say, I have a data object which has a member of class 'flags' which bundles a large number of properties that define how to handle the data object. How else would you call this?

Efrain
  • 3,248
  • 4
  • 34
  • 61

2 Answers2

12

In the book Framework Design Guidelines, which is what FxCop is based on, the authors say that using Flag or Flags is a bad idea. Their alternative suggestion is that when naming enumerations that you use a singular name for standard enums and a plural name for bit field (flags) enums.

For example if you wanted to create an enum listing different visibilities then you would name it Visibilities instead of VisibilityFlags or Visibility:

[Flags]
public enum Visibilities {
   Public,
   Private
}

The only items considered flags in .NET by the authors are these bitfield enumerations due to the keyword Flags attribute.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Jason Moore
  • 3,294
  • 15
  • 18
  • 1
    I'm using a BitArray, to represent... a set of flags. Putting 'flags' in the name seems quite natural. This seems like a bit of a draconian rule to me. – Andy Oct 04 '12 at 08:15
  • Reviving an old Q&A - I am running into this issue because the business really uses the term "flag" and I want to represent that in the domain model. I am sure that book refers to flag as a bad idea when it comes to the programming concept of an on/off field, and that is fine. But implementing this rule in FXCop such that *any* use of flag is considered obsolete is an even worse idea IMHO - just look at google and find that the term "flag" is still in use for many things, e.g. country flags, or flagging a post. – chiccodoro Jul 05 '13 at 14:03
  • Ironically, Stackoverflow *has* a concept of flagging posts; imagining how the SO team is forced to use a term different from "flag" for representing post flags in their domain model just to satisfy FXCop is a bit worrying... – chiccodoro Jul 05 '13 at 14:05
3

I would say that the property should be named aptly, and that the term Flags characterises the property rather than describing it.

Flag or Flags | There is no replacement term. Do not use.

For instance, Flags is generally used with enumerations (that are decorated with the appropriate attribute) and we certainly don't need to explicitly state so within the name / identifier of a property:

[Flags]
enum StorageMode
{
    None = 0,
    Next = 1,
    ...
    Last = 32
}

class StorableItem
{
    public StorageMode StorageMode { get; set; }
}

But, in your case, I get the feeling that whatever is named, or contains within its name, Flags, isn't actually a set of flags in the above sense - which just brings up another reason as to why to avoid it.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129