Coding Conventions - Naming Enums
In Java, enums should follow a UPPER_CASE
naming convention just like constants. This reinforces their immutable nature in the eyes of fellow developers and aligns with the Java Language Specification:
For enums with compound names, remember that readability is king. Therefore, you can opt for UPPER_CASE
or play with Mixed_Case
to make it easy on the eyes:
Using meaningful and descriptive names is better than sticking a dull "Enum" at the end.
Avoid confusion with class naming
Sometimes, enums could share a similar identity with a class, causing a naming clash. To avoid confusion, differentiate with context and usage:
With distinguishable and clear names, one can easily tell a model or utility (NetworkConnection
) apart from a set of predefined options (NetworkConnectionType
).
Balancing brevity and clarity
In your pursuit of the perfect enum name, always strike a balance between brevity and descriptiveness:
State
is simple and conveys as much information as the verbose StateOfObject
.
Making it chime with methods
Remember that the naming choice for enums can affect their interaction with certain methods like .name()
, which returns the enum constant's name as a String
:
Capitalizing your enums correctly ensures both the consistency and predictability of your code.
Was this article helpful?