Monday, November 7, 2005

Code Advice #7: Capitalize your Enum constants

WARNING: This blog entry was imported from my old blog on (which used different blogging software), so formatting and links may not be correct.

(See intro for a background and caveats on these coding advice blog entries.)

I've started using the Java 5 language features - and loving every minute of it. The other day I wrote an enum. (Tip: Did you know that there are two new classes,
EnumMap and
EnumSet, which lets you write "bit flags" with int-performance and with type safety?).

But, I've seen enum constants capitalized in different ways. For example, in
technical article enum constants are in all lower case, in
article they're all in upper case, and finally I've seen people argue (can't find a link
at the moment) that enum constants should have their own capitalization: initial cap. The latter
form is interesting, but when I read about it, no solid argument for why it should be done that way was offered, so it seems better to not confuse it with Class and Interface name capitalization.

We should pick one convention. And I think that convention should be: All uppercase, same
as constants today. Why? It makes a lot of sense, because enum constants ARE final like the old public static final's you're used to capitalizing.


public enum StopLight { red, amber, green };
public enum StopLight { Red, Amber, Green };
public enum StopLight { RED, AMBER, GREEN };

1 comment:

  1. I too have been using the new language features quite a lot, and also *love* them! I totally agree that enum constants should be all UPPER_CASE. I want to add a caveat, which is that multiple word constants should use the underscore character to separate them.