编码约定 - 命名枚举
Java 中有命名枚举的约定吗?
我的偏好是枚举是一种类型。因此,例如,您有一个枚举,
Fruit{Apple,Orange,Banana,Pear, ... }
NetworkConnectionType{LAN,Data_3g,Data_4g, ... }
我反对命名它:
FruitEnum
NetworkConnectionTypeEnum
我知道很容易挑选出哪些文件是枚举,但是您还会有:
NetworkConnectionClass
FruitClass
另外,是否有一个很好的文档描述了相同的常量,其中声明它们等等?
Is there a convention for naming enumerations in Java?
My preference is that an enum is a type. So, for instance, you have an enum
Fruit{Apple,Orange,Banana,Pear, ... }
NetworkConnectionType{LAN,Data_3g,Data_4g, ... }
I am opposed to naming it:
FruitEnum
NetworkConnectionTypeEnum
I understand it is easy to pick off which files are enums, but then you would also have:
NetworkConnectionClass
FruitClass
Also, is there a good document describing the same for constants, where to declare them, etc.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
枚举是类,应该遵循类的约定。枚举的实例是常量,应该遵循常量的约定。因此,
没有理由编写
FruitEnum
和FruitClass
一样。您只是浪费了四个(或五个)字符,没有添加任何信息。Java™ 教程推荐并使用此方法例子本身。
Enums are classes and should follow the conventions for classes. Instances of an enum are constants and should follow the conventions for constants. So
There is no reason for writing
FruitEnum
any more thanFruitClass
. You are just wasting four (or five) characters that add no information.This approach is recommended by and used in the The Java™ Tutorial's examples themselves.
这可能不会让我结识很多新朋友,但应该补充的是,C# 人员有不同的指导方针:枚举实例是“Pascal 大小写”(大小写混合)。请参阅 stackoverflow 讨论 和 MSDN 枚举类型命名指南。
当我们与 C# 系统交换数据时,我很想准确地复制它们的枚举,而忽略了 Java 的“常量名称大写”约定。考虑一下,我认为将枚举实例限制为大写没有多大价值。出于某些目的,.name() 是获取枚举常量的可读表示的便捷快捷方式,并且混合大小写的名称看起来会更好。
所以,是的,我敢质疑 Java 枚举命名约定的价值。事实上,“编程世界的另一半”确实使用了不同的风格,这让我认为怀疑我们自己的宗教是合理的。
This will probably not make me a lot of new friends, but it should be added that the C# people have a different guideline: The enum instances are "Pascal case" (upper/lower case mixed). See stackoverflow discussion and MSDN Enumeration Type Naming Guidelines.
As we are exchanging data with a C# system, I am tempted to copy their enums exactly, ignoring Java's "constants have uppercase names" convention. Thinking about it, I don't see much value in being restricted to uppercase for enum instances. For some purposes .name() is a handy shortcut to get a readable representation of an enum constant and a mixed case name would look nicer.
So, yes, I dare question the value of the Java enum naming convention. The fact that "the other half of the programming world" does indeed use a different style makes me think it is legitimate to doubt our own religion.
如前所述,根据 Oracle 网站上的文档,枚举实例应为大写 (http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html)。
但是,在浏览 Oracle 网站上的 JavaEE7 教程时 (http:// www.oracle.com/technetwork/java/javaee/downloads/index.html),我偶然发现了“Duke's bookstore”教程和一堂课(
tutorial\examples\case-studies\dukes- bookstore\src\main\java\javaeetutorial\dukesbookstore\components\AreaComponent.java
),我找到了以下枚举定义:根据约定,它应该看起来像:
所以有时甚至是 Oracle 的人贸易洽谈便利。
As already stated, enum instances should be uppercase according to the docs on the Oracle website (http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html).
However, while looking through a JavaEE7 tutorial on the Oracle website (http://www.oracle.com/technetwork/java/javaee/downloads/index.html), I stumbled across the "Duke's bookstore" tutorial and in a class (
tutorial\examples\case-studies\dukes-bookstore\src\main\java\javaeetutorial\dukesbookstore\components\AreaComponent.java
), I found the following enum definition:According to the conventions, it should have looked like:
So it seems even the guys at Oracle sometimes trade convention with convenience.
在我们的代码库中;我们通常在它们所属的类中声明枚举。
因此,对于您的 Fruit 示例,我们将有一个 Fruit 类,其中有一个名为 Fruits 的枚举。
在代码中引用它看起来像这样:
Fruit.Fruits.Apple、Fruit.Fruits.Pear
等。常量遵循同一行,它们要么在它们所属的类中定义。相关(例如
Fruit.ORANGE_BUSHEL_SIZE
);或者,如果它们在名为“ConstantManager”的类(或等效的;如ConstantManager.NULL_INT
)中应用于系统范围(即整数的等效“空值”)。 (旁注;我们所有的常量均为大写)一如既往,您的编码标准可能与我的不同;所以YMMV。
In our codebase; we typically declare enums in the class that they belong to.
So for your Fruit example, We would have a Fruit class, and inside that an Enum called Fruits.
Referencing it in the code looks like this:
Fruit.Fruits.Apple, Fruit.Fruits.Pear
, etc.Constants follow along the same line, where they either get defined in the class to which they're relevant (so something like
Fruit.ORANGE_BUSHEL_SIZE
); or if they apply system-wide (i.e. an equivalent "null value" for ints) in a class named "ConstantManager" (or equivalent; likeConstantManager.NULL_INT
). (side note; all our constants are in upper case)As always, your coding standards probably differ from mine; so YMMV.
它们仍然是类型,所以我总是使用与类相同的命名约定。
我绝对不会在名称中添加“Class”或“Enum”。如果您同时拥有
FruitClass
和FruitEnum
,那么就会出现其他问题,您需要更具描述性的名称。我正在尝试考虑哪种代码会导致需要两者,并且似乎应该有一个带有子类型而不是枚举的Fruit
基类。 (但这只是我自己的猜测,您的情况可能与我想象的不同。)我可以找到的命名常量的最佳参考来自 变量教程:
They're still types, so I always use the same naming conventions I use for classes.
I definitely would frown on putting "Class" or "Enum" in a name. If you have both a
FruitClass
and aFruitEnum
then something else is wrong and you need more descriptive names. I'm trying to think about the kind of code that would lead to needing both, and it seems like there should be aFruit
base class with subtypes instead of an enum. (That's just my own speculation though, you may have a different situation than what I'm imagining.)The best reference that I can find for naming constants comes from the Variables tutorial:
如果我可以添加 0.02 美元,我更喜欢在 C 中使用 PascalCase 作为枚举值。
在 C 中,它们基本上是全局的,与 PeerConnected 相比,PEER_CONNECTED 真的很累。
呼吸新鲜空气。
从字面上看,它让我呼吸更轻松。
在 Java 中,只要从另一个类静态导入它们,就可以使用原始枚举名称。
现在,您可以使用已以不同方式限定的非限定名称。
我目前正在(考虑)将一些 C 代码移植到 Java,并且目前在选择 Java 约定(更冗长、更冗长、更难看)和我的 C 风格之间“左右为难”。
PeerConnected 将变为 PeerState.CONNECTED,除非在 switch 语句中为 CONNECTED。
现在对于后一种约定有很多话要说,它看起来确实不错,但某些“惯用短语”,例如
if (s == PeerAvailable)
变得像if (s == PeerState.AVAILABLE )
怀旧的是,这对我来说失去了意义。我想我仍然更喜欢 Java 风格,因为它清晰,但我很难看那些令人尖叫的代码。
现在我意识到 PascalCase 已经在 Java 中广泛使用,但非常令人困惑,它实际上并非如此,只是有点不合适。
If I can add my $0.02, I prefer using PascalCase as enum values in C.
In C, they are basically global, and PEER_CONNECTED gets really tiring as opposed to PeerConnected.
Breath of fresh air.
Literally, it makes me breathe easier.
In Java, it is possible to use raw enum names as long as you static import them from another class.
Now, you can use the unqualified names, that you qualified in a different way already.
I am currently (thinking) about porting some C code to Java and currently 'torn' between choosing Java convention (which is more verbose, more lengthy, and more ugly) and my C style.
PeerConnected would become PeerState.CONNECTED except in switch statements, where it is CONNECTED.
Now there is much to say for the latter convention and it does look nice but certain "idiomatic phrases" such as
if (s == PeerAvailable)
become likeif (s == PeerState.AVAILABLE)
and nostalgically, this is a loss of meaning to me.I think I still prefer the Java style because of clarity but I have a hard time looking at the screaming code.
Now I realize PascalCase is already widely used in Java but very confusing it would not really be, just a tad out of place.
(大约)就像
这样说,我猜全部大写严格来说更正确,但我仍然使用类名约定,因为我讨厌所有大写
is (approximately) like saying
so I guess the all caps is strictly more correct, but still I use the class name convention since I hate all caps wherever