Ilya Luzyanin
Ilya Luzyanin

Reputation: 8110

How to name a collection of flags?

For example I have the following flag enum:

[Flags]
public enum Colors
{
    Red = 1,
    Green = 2,
    Blue = 4
}    

According to MS Guidelines:

DO name flag enums with plural nouns or noun phrases and simple enums with singular nouns or noun phrases.

So I've used plural form here. Now, there is another guideline to name your collections in plural form:

DO name collection properties with a plural phrase describing the items in the collection instead of using a singular phrase followed by "List" or "Collection."

I have a class something like this:

public class Foo
{
    public IEnumerable<Colors> Colors { get; set; }
}

The problem is it gets very confusing when I try to work with separate items in that collection - they're also colors. So how should I name a collection of flags then?

EDIT:

Ok, the example is not very clear, I agree. Maybe this one is better:

[Flags]
public enum Operations
{
    TextFormatting = 1,
    SpellChecking = 2,
    Translation = 4
}

public class TextProcessingParameters
{
    public IEnumerable<Operations> Operations { get; set; }
    // other parameters, including parameters for different operations
}

After text processor has finished, it has several results - one for each operations in Operations collection (already confusing), e.g. one for SpellChecking AND TextFormatting, and another for Translation only.

Upvotes: 12

Views: 748

Answers (2)

William
William

Reputation: 2191

While agreeing with the question comments that something doesn't feel quite right, I'd suggest that if the enum name is chosen more carefully to reflect the "component" nature of each item it can represent, the problem seems to go away.

For example, the original renamed:

[Flags]
public enum ColorComponents
{
    Red = 1,
    Green = 2,
    Blue = 4
}

public class Foo
{
    public IEnumerable<ColorComponents> Colors { get; set; }
}

And the updated example renamed:

[Flags]
public enum OperationComponents
{
    TextFormatting = 1,
    SpellChecking = 2,
    Translation = 4
}

public class TextProcessingParameters
{
    public IEnumerable<OperationComponents> Operations { get; set; }
    // other parameters, including parameters for different operations
}

You can also take a slightly different approach by renaming the collection to reflect the compositional aspect of each item in the collection:

[Flags]
public enum Operations
{
    TextFormatting = 1,
    SpellChecking = 2,
    Translation = 4
}

public class TextProcessingParameters
{
    public IEnumerable<Operations> OperationSets { get; set; }
    // other parameters, including parameters for different operations
}

The first approach seems slightly cleaner, though.

Upvotes: 4

Heinzi
Heinzi

Reputation: 172380

I would expect Operations to be a list of Operation, not a list of Operations. Unfortunately, you cannot pluralize Operation twice.

Thus, I'd take the pragmatic approach an invent a new word for your flag enum, which is

  • grammatically singular but
  • still represents the combination of individual enum values rather than a single one of them.

For the sake of argument, let's call the enum OpCombination -- a combination of operations. Then you can name the list naturally:

public IEnumerable<OpCombination> OpCombinations { get; set; }

Upvotes: 1

Related Questions