Coding Conventions - Naming Enums
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.?
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.
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.
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.
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.
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.
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.
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
精彩评论