Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CA1726: FxCop Forbidden Word: Flags

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?

like image 500
Efrain Avatar asked Apr 20 '11 11:04

Efrain


2 Answers

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.

like image 142
Jason Moore Avatar answered Nov 18 '22 19:11

Jason Moore


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.

like image 36
Grant Thomas Avatar answered Nov 18 '22 20:11

Grant Thomas