Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name a collection of flags?

Tags:

c#

enums

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.

like image 684
Ilya Luzyanin Avatar asked Apr 15 '15 18:04

Ilya Luzyanin


1 Answers

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.

like image 139
William Avatar answered Sep 22 '22 16:09

William