枚举命名约定 - 复数
尽管我在 枚举和匹配属性的C#命名约定
我发现我倾向于以复数形式命名枚举,然后将它们“使用”为单数,例如:
public enum EntityTypes {
Type1, Type2
}
public class SomeClass {
/*
some codes
*/
public EntityTypes EntityType {get; set;}
}
当然它有效,这是我的风格,但任何人都可以找到潜力这样的约定有问题吗?不过,我确实有一个带有“状态”一词的“丑陋”命名:
public enum OrderStatuses {
Pending, Fulfilled, Error, Blah, Blah
}
public class SomeClass {
/*
some codes
*/
public OrderStatuses OrderStatus {get; set;}
}
附加信息: 也许我的问题不够清楚。在命名我定义的枚举类型的变量时,我经常必须认真思考。我知道最佳实践,但这无助于减轻我命名这些变量的工作。
我不可能将所有枚举属性(例如“Status”)公开为“MyStatus”。
我的问题:任何人都可以发现我上述约定的潜在问题吗? 这与最佳实践无关。
问题改述:
好吧,我想我应该这样问问题:有人能想出一种很好的通用方法来命名枚举类型,例如使用时,枚举“实例”的命名会非常简单吗?
I'm asking this question despite having read similar but not exactly what I want at C# naming convention for enum and matching property
I found I have a tendency to name enums in plural and then 'use' them as singular, example:
public enum EntityTypes {
Type1, Type2
}
public class SomeClass {
/*
some codes
*/
public EntityTypes EntityType {get; set;}
}
Of course it works and this is my style, but can anyone find potential problem with such convention? I do have an "ugly" naming with the word "Status" though:
public enum OrderStatuses {
Pending, Fulfilled, Error, Blah, Blah
}
public class SomeClass {
/*
some codes
*/
public OrderStatuses OrderStatus {get; set;}
}
Additional Info:
Maybe my question wasn't clear enough. I often have to think hard when naming the variables of the my defined enum types. I know the best practice, but it doesn't help to ease my job of naming those variables.
I can't possibly expose all my enum properties (say "Status") as "MyStatus".
My question: Can anyone find potential problem with my convention described above? It is NOT about best practice.
Question rephrase:
Well, I guess I should ask the question this way: Can someone come out a good generic way of naming the enum type such that when used, the naming of the enum 'instance' will be pretty straightforward?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Microsoft 建议对
Enum
使用单数,除非Enum
表示位字段(使用FlagsAttribute
以及)。请参阅枚举类型命名约定(Microsoft 的子集命名指南)。为了回应您的澄清,我认为以下任何一项都没有问题:
或
Microsoft recommends using singular for
Enum
s unless theEnum
represents bit fields (use theFlagsAttribute
as well). See Enumeration Type Naming Conventions (a subset of Microsoft's Naming Guidelines).To respond to your clarification, I see nothing wrong with either of the following:
or
我一开始用复数命名枚举,但后来改为单数。在它们的使用环境中似乎更有意义。
比较:
我发现单数形式在上下文中听起来更自然。我们一致认为,当声明发生在一个地方的枚举时,我们会想到“这是一组无论什么”,但是当使用它时,大概是在许多地方,我们会想到“这是一个无论如何” 。
I started out naming enums in the plural but have since changed to singular. Just seems to make more sense in the context of where they're used.
Compare to:
I find the singular form to sound more natural in context. We are in agreement that when declaring the enum, which happens in one place, we're thinking "this is a group of whatevers", but when using it, presumably in many places, that we're thinking "this is one whatever".
这种情况实际上并不适用于复数。
enum
显示某事物的属性。我举一个例子:思考它:
你可以有一种类型,但尝试以倍数而不是复数的形式来 Humour.Sarcasm
而不是
Humours { Irony, Sarcasm }
你有幽默感,你没有幽默感。
The situation never really applies to plural.
An
enum
shows an attribute of something or another. I'll give an example:You can have one type, but try think of it in the multiple, rather than plural:
Humour.Irony | Humour.Sarcasm
Rather than
Humours { Irony, Sarcasm }
You have a sense of humour, you don't have a sense of humours.
这是我不同意该公约足以反对它的少数几个地方之一。说实话,我讨厌枚举的定义和它的实例可以具有相同的名称。我在所有枚举后面加上“Enum”,因为它清楚地表明了它在任何给定用法中的上下文。 IMO 它使代码更具可读性。
没有人会混淆什么是枚举和什么是它的实例。
This is one of the few places that I disagree with the convention enough to go against it. TBH, I HATE that the definition of an enum and the instance of it can have the same name. I postfix all of my Enums with "Enum" specifically because it makes it clear what the context of it is in any given usage. IMO it makes the code much more readable.
Nobody will ever confuse what is the enum and what is the instance of it.
一般来说,最佳实践建议是单数,除了那些附加了 [Flags] 属性的枚举(因此可以包含位字段),它们应该是复数。
阅读您编辑的问题后,我感觉您可能认为属性名称或变量名称必须与枚举类型名称不同......事实并非如此。下面的就完全没问题了...
In general, the best practice recommendation is singular, except for those enums that have the [Flags] attribute attached to them, (and which therefore can contain bit fields), which should be plural.
After reading your edited question, I get the feeling you may think the property name or variable name has to be different from the enum type name... It doesn't. The following is perfectly fine...
如果您尝试编写如下所示的简单但禁止的代码:
您的选择是:
忽略 MS 建议并在枚举名称上使用前缀或后缀:
将枚举定义移到类之外,最好移到另一个类中。这是上述问题的一个简单解决方案:
If you are trying to write straightforward, yet forbidden code like this:
Your options are:
Ignore the MS recommendation and use a prefix or suffix on the enum name:
Move the enum definition outside the class, preferably into another class. Here is an easy solution to the above:
最佳实践 - 使用单数。您有一个组成枚举的项目列表。当您说
Versions.1_0
时,使用列表中的项目听起来很奇怪。说Version.1_0
更有意义,因为只有一个 1_0 版本。Best Practice - use singular. You have a list of items that make up an Enum. Using an item in the list sounds strange when you say
Versions.1_0
. It makes more sense to sayVersion.1_0
since there is only one 1_0 Version.来得有点晚了...
您的问题与
你将枚举定义放在类之外,这允许你为枚举和属性使用相同的名称:
在这种情况下,我会遵循MS 指南并为枚举使用单数名称(标志为复数名称)。这可能是最简单的解决方案。
我的问题(在其他问题中)是当枚举在类的范围内定义,防止使用完全以枚举命名的属性。
Coming in a bit late...
There's an important difference between your question and the one you mention (which I asked ;-):
You put the enum definition out of the class, which allows you to have the same name for the enum and the property:
In this case, I'd follow the MS guidelins and use a singular name for the enum (plural for flags). It's probaby the easiest solution.
My problem (in the other question) is when the enum is defined in the scope of the class, preventing the use of a property named exactly after the enum.
在枚举声明中使用复数的原因是(在声明时)我们用多个值声明它,所以复数似乎很好......
但我们忽略了这样一个事实:枚举在声明时指定了它可以具有什么值(来自给定的值集)。这并不意味着该枚举的实例将存储多个值......
当我们写:
枚举天数 { MON, TUE, WED, THU, FRI, SAT, SUN};
我们将其设为复数,因为提供了多个值。
然而,当使用(Days day = Days.MON;)时,我们完全忽略该枚举的实例应该具有单个值......
所以当我们写:
枚举 Day { MON, TUE, WED, THU, FRI, SAT, SUN };
我们的意思是有一个枚举可以将任意一天作为其值,因此单数更合适。
虽然(上面已经描述过),要在不使用单数名称的情况下解决这个问题,可以使用任何类型的指标,例如 DayEnum 或 EDay(我更喜欢第二种)....
The reason for using a plural for a enum declaration is the fact that ( at the time of declaration ) we declare it with multiple values, so plural seems good...
But we ignore the fact that enum when declared specifies what value it can have ( from the given set of values ). It doesn't mean that the instance of that enum will store multiple values.....
When we write:
enum Days { MON, TUE, WED, THU, FRI, SAT, SUN};
We are making it plural because of the multiple values provide..
However when used (Days day = Days.MON; ) we completely ignore that instance of that enum is supposed to have a single value....
So when we write :
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN };
We mean that there is a enum that can have any one day as its value, so singular is more appropriate.
Although (already described above ), to get around this without using singular names is by using any kind of Indicator, like DayEnum or EDay ( i prefer the second one)....
在另一个线程上 枚举和匹配属性的 C# 命名约定有人指出我认为这是一个非常好的主意:
“我知道我的建议违反了 .NET 命名约定,但我个人在枚举前加上“E”前缀,在枚举标志前加上“F”(类似于我们在接口前加上“”)我')。”
On the other thread C# naming convention for enum and matching property someone pointed out what I think is a very good idea:
"I know my suggestion goes against the .NET Naming conventions, but I personally prefix enums with 'E' and enum flags with 'F' (similar to how we prefix Interfaces with 'I')."