编码约定 - 命名枚举

发布于 2024-09-05 20:29:57 字数 383 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

ㄖ落Θ余辉 2024-09-12 20:29:57

枚举是类,应该遵循类的约定。枚举的实例是常量,应该遵循常量的约定。因此,

enum Fruit {APPLE, ORANGE, BANANA, PEAR};

没有理由编写 FruitEnumFruitClass 一样。您只是浪费了四个(或五个)字符,没有添加任何信息。

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

enum Fruit {APPLE, ORANGE, BANANA, PEAR};

There is no reason for writing FruitEnum any more than FruitClass. 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.

離人涙 2024-09-12 20:29:57

这可能不会让我结识很多新朋友,但应该补充的是,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.

白芷 2024-09-12 20:29:57

如前所述,根据 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),我找到了以下枚举定义:

private enum PropertyKeys {
    alt, coords, shape, targetImage;
}

根据约定,它应该看起来像:

public enum PropertyKeys {
    ALT("alt"), COORDS("coords"), SHAPE("shape"), TARGET_IMAGE("targetImage");

    private final String val;

    private PropertyKeys(String val) {
        this.val = val;
    }

    @Override
    public String toString() {
        return val;
    }
}

所以有时甚至是 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:

private enum PropertyKeys {
    alt, coords, shape, targetImage;
}

According to the conventions, it should have looked like:

public enum PropertyKeys {
    ALT("alt"), COORDS("coords"), SHAPE("shape"), TARGET_IMAGE("targetImage");

    private final String val;

    private PropertyKeys(String val) {
        this.val = val;
    }

    @Override
    public String toString() {
        return val;
    }
}

So it seems even the guys at Oracle sometimes trade convention with convenience.

z祗昰~ 2024-09-12 20:29:57

在我们的代码库中;我们通常在它们所属的类中声明枚举。

因此,对于您的 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; like ConstantManager.NULL_INT). (side note; all our constants are in upper case)

As always, your coding standards probably differ from mine; so YMMV.

路弥 2024-09-12 20:29:57

它们仍然是类型,所以我总是使用与类相同的命名约定。

我绝对不会在名称中添加“Class”或“Enum”。如果您同时拥有 FruitClassFruitEnum ,那么就会出现其他问题,您需要更具描述性的名称。我正在尝试考虑哪种代码会导致需要两者,并且似乎应该有一个带有子类型而不是枚举的 Fruit 基类。 (但这只是我自己的猜测,您的情况可能与我想象的不同。)

我可以找到的命名常量的最佳参考来自 变量教程

如果您选择的名称仅包含一个单词,请以全部小写字母拼写该单词。如果它由多个单词组成,则将每个后续单词的第一个字母大写。 gearRatio 和 currentGear 名称是该约定的主要示例。如果您的变量存储常量值,例如 static final int NUM_GEARS = 6,则约定会略有变化,将每个字母大写并使用下划线字符分隔后续单词。按照惯例,下划线字符不会在其他地方使用。

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 a FruitEnum 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 a Fruit 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:

If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.

如梦亦如幻 2024-09-12 20:29:57

如果我可以添加 0.02 美元,我更喜欢在 C 中使用 PascalCase 作为枚举值。

在 C 中,它们基本上是全局的,与 PeerConnected 相比,PEER_CONNECTED 真的很累。

呼吸新鲜空气。

从字面上看,它让我呼吸更轻松。

在 Java 中,只要从另一个类静态导入它们,就可以使用原始枚举名称。

import static pkg.EnumClass.*;

现在,您可以使用已以不同方式限定的非限定名称。

我目前正在(考虑)将一些 C 代码移植到 Java,并且目前在选择 Java 约定(更冗长、更冗长、更难看)和我的 C 风格之间“左右为难”。

PeerConnected 将变为 PeerState.CONNECTED,除非在 switch 语句中为 CONNECTED。

现在对于后一种约定有很多话要说,它看起来确实不错,但某些“惯用短语”,例如 if (s == PeerAvailable) 变得像 if (s == PeerState.AVAILABLE ) 怀旧的是,这对我来说失去了意义。

我想我仍然更喜欢 Java 风格,因为它清晰,但我很难看那些令人尖叫的代码。

现在我意识到 PascalCase 已经在 J​​ava 中广泛使用,但非常令人困惑,它实际上并非如此,只是有点不合适。

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.

import static pkg.EnumClass.*;

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 like if (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.

错爱 2024-09-12 20:29:57
enum MyEnum {VALUE_1,VALUE_2}

(大约)就像

class MyEnum {

    public static final MyEnum VALUE_1 = new MyEnum("VALUE_1");
    public static final MyEnum VALUE_2 = new MyEnum("VALUE_2");

    private final name;

    private MyEnum(String name) {
        this.name = name;
    }

    public String name() { return this.name }
}

这样说,我猜全部大写严格来说更正确,但我仍然使用类名约定,因为我讨厌所有大写

enum MyEnum {VALUE_1,VALUE_2}

is (approximately) like saying

class MyEnum {

    public static final MyEnum VALUE_1 = new MyEnum("VALUE_1");
    public static final MyEnum VALUE_2 = new MyEnum("VALUE_2");

    private final name;

    private MyEnum(String name) {
        this.name = name;
    }

    public String name() { return this.name }
}

so I guess the all caps is strictly more correct, but still I use the class name convention since I hate all caps wherever

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文