如何命名 collection 个旗帜?
How to name a collection of flags?
例如我有以下标志枚举:
[Flags]
public enum Colors
{
Red = 1,
Green = 2,
Blue = 4
}
DO name flag enums with plural nouns or noun phrases and simple enums with singular nouns or noun phrases.
所以我在这里使用了复数形式。现在,another guideline 以复数形式命名您的 collection:
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."
我有一个 class 像这样的东西:
public class Foo
{
public IEnumerable<Colors> Colors { get; set; }
}
问题是,当我尝试在 collection 中处理单独的项目时,它变得非常混乱 - 它们也是 colors
。
那么我应该如何命名 collection 个标志呢?
编辑:
好吧,这个例子不是很清楚,我同意。也许这个更好:
[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
}
文本处理器完成后,它有几个结果 - Operations
collection 中的每个 operations
(已经令人困惑),例如一个用于 SpellChecking
AND TextFormatting
,另一个仅用于 Translation
。
我希望 Operations
是 Operation
的列表,而不是 Operations
的列表。不幸的是,您不能将 Operation
复数化两次。
因此,我会采用务实的方法为您的标志枚举发明一个新词,即
- 在语法上是单数但
- 仍然表示单个枚举值的组合,而不是其中的一个。
为了论证,我们称枚举为 OpCombination
-- 操作的组合。然后你可以自然地命名列表:
public IEnumerable<OpCombination> OpCombinations { get; set; }
虽然同意问题的评论,认为有些事情不太对劲,但我建议如果更仔细地选择枚举名称以反映它可以代表的每个项目的 "component" 性质,问题似乎消失了。
例如原改名:
[Flags]
public enum ColorComponents
{
Red = 1,
Green = 2,
Blue = 4
}
public class Foo
{
public IEnumerable<ColorComponents> Colors { get; set; }
}
更新后的示例重命名为:
[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
}
您还可以采取稍微不同的方法,通过重命名集合以反映集合中每个项目的组成方面:
[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
}
不过,第一种方法似乎更简洁一些。
例如我有以下标志枚举:
[Flags]
public enum Colors
{
Red = 1,
Green = 2,
Blue = 4
}
DO name flag enums with plural nouns or noun phrases and simple enums with singular nouns or noun phrases.
所以我在这里使用了复数形式。现在,another guideline 以复数形式命名您的 collection:
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."
我有一个 class 像这样的东西:
public class Foo
{
public IEnumerable<Colors> Colors { get; set; }
}
问题是,当我尝试在 collection 中处理单独的项目时,它变得非常混乱 - 它们也是 colors
。
那么我应该如何命名 collection 个标志呢?
编辑:
好吧,这个例子不是很清楚,我同意。也许这个更好:
[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
}
文本处理器完成后,它有几个结果 - Operations
collection 中的每个 operations
(已经令人困惑),例如一个用于 SpellChecking
AND TextFormatting
,另一个仅用于 Translation
。
我希望 Operations
是 Operation
的列表,而不是 Operations
的列表。不幸的是,您不能将 Operation
复数化两次。
因此,我会采用务实的方法为您的标志枚举发明一个新词,即
- 在语法上是单数但
- 仍然表示单个枚举值的组合,而不是其中的一个。
为了论证,我们称枚举为 OpCombination
-- 操作的组合。然后你可以自然地命名列表:
public IEnumerable<OpCombination> OpCombinations { get; set; }
虽然同意问题的评论,认为有些事情不太对劲,但我建议如果更仔细地选择枚举名称以反映它可以代表的每个项目的 "component" 性质,问题似乎消失了。
例如原改名:
[Flags]
public enum ColorComponents
{
Red = 1,
Green = 2,
Blue = 4
}
public class Foo
{
public IEnumerable<ColorComponents> Colors { get; set; }
}
更新后的示例重命名为:
[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
}
您还可以采取稍微不同的方法,通过重命名集合以反映集合中每个项目的组成方面:
[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
}
不过,第一种方法似乎更简洁一些。