Three Core Principles
Excellent enum naming follows three core principles, ensuring code remains clear and consistent across different scenarios.
Readable
Enum names should clearly express business meaning, allowing developers to understand their purpose and context at a glance.
- • Use complete words rather than abbreviations
- • Avoid ambiguous names like STATUS1, TYPE_A
- • Reflect business semantics rather than technical implementation
- • Keep naming length moderate (2-4 words)
Predictable
Follow consistent naming patterns, enabling team members to predict and guess enum value names.
- • Use consistent naming style within the same enum
- • Maintain consistent verb tense and singular/plural forms
- • Use team-agreed prefixes/suffixes
- • Arrange enum values in logical order
Portable
Consider cross-language and cross-system compatibility to ensure enums work correctly in different environments.
- • Avoid using language reserved words
- • Consider JSON/API serialization formats
- • Support database enum types
- • Maintain consistent naming between frontend and backend
Common Naming Patterns
Choosing appropriate naming patterns based on different use cases makes code clearer and more consistent.
State Pattern
Represents different states of an object, typically using adjectives or past participles.
Recommended
enum OrderStatus {
PENDING, // Pending
CONFIRMED, // Confirmed
PROCESSING, // Processing
SHIPPED, // Shipped
DELIVERED, // Delivered
CANCELLED // Cancelled
}
Use past participles or adjectives to clearly express state
Avoid
enum OrderStatus {
STATUS_1, // Unclear
WAIT, // Inappropriate verb form
SEND_OUT, // Should use SHIPPED
OK // Too vague
}
Avoid numbering, verb infinitives, and vague naming
Action Pattern
Represents executable operations, typically using verbs.
Recommended
enum UserAction {
CREATE, // Create
READ, // Read
UPDATE, // Update
DELETE, // Delete
EXPORT, // Export
IMPORT // Import
}
Use clear verbs, following CRUD conventions
Avoid
enum UserAction {
DO_CREATE, // Redundant DO_ prefix
READING, // Inconsistent tense
UPD, // Excessive abbreviation
DEL // Incomplete
}
Avoid redundant prefixes, tense confusion, and abbreviations
Type Pattern
Represents object categories or classifications, typically using nouns.
Recommended
enum PaymentMethod {
CREDIT_CARD, // Credit card
DEBIT_CARD, // Debit card
PAYPAL, // PayPal
BANK_TRANSFER, // Bank transfer
CASH, // Cash
CRYPTOCURRENCY // Cryptocurrency
}
Use specific nouns, easy to understand
Avoid
enum PaymentMethod {
METHOD_1, // Meaningless numbering
CARD, // Not specific enough
PP, // Excessive abbreviation
BY_BANK // Inconsistent format
}
Avoid numbering, vague naming, and inconsistent formats
Level Pattern
Represents priority, level, or degree.
Recommended
enum Priority {
CRITICAL, // Critical
HIGH, // High
MEDIUM, // Medium
LOW, // Low
TRIVIAL // Trivial
}
enum LogLevel {
FATAL,
ERROR,
WARN,
INFO,
DEBUG,
TRACE
}
Sorted by decreasing importance
Avoid
enum Priority {
PRIORITY_1, // Using numbers is not intuitive
HIGH,
MIDDLE, // Should use MEDIUM
LESS // Should use LOW
}
Avoid numeric numbering and non-standard naming
Cross-Language Naming Conventions
Different programming languages have their own naming conventions. In multi-language projects, it's necessary to maintain semantic consistency while respecting each language's conventions.
Naming Style Comparison Table
| Language | Enum Name | Enum Value | Example |
|---|---|---|---|
| Java | PascalCase | UPPER_SNAKE_CASE | OrderStatus.PENDING |
| C# | PascalCase | PascalCase | OrderStatus.Pending |
| Python | PascalCase | UPPER_SNAKE_CASE | OrderStatus.PENDING |
| TypeScript | PascalCase | PascalCase or UPPER | OrderStatus.Pending |
| Rust | PascalCase | PascalCase | OrderStatus::Pending |
| Go | PascalCase | PascalCase (with prefix) | OrderStatusPending |
| C/C++ | PascalCase or snake_case | UPPER_SNAKE_CASE | ORDER_STATUS_PENDING |
Cross-Language Consistency Strategy
In multi-language projects, maintaining enum semantic consistency is more important than format consistency.
Core Semantic Unification
// Java
enum OrderStatus { PENDING, CONFIRMED, SHIPPED }
# Python
class OrderStatus(Enum):
PENDING = "pending"
CONFIRMED = "confirmed"
SHIPPED = "shipped"
// TypeScript
enum OrderStatus {
Pending = "pending",
Confirmed = "confirmed",
Shipped = "shipped"
}
Use unified lowercase strings for values to facilitate JSON serialization
Documentation Comment Synchronization
// Java
/** Order Status */
public enum OrderStatus {
/** Pending */
PENDING,
/** Confirmed */
CONFIRMED
}
# Python
class OrderStatus(Enum):
"""Order Status"""
PENDING = "pending" # Pending
CONFIRMED = "confirmed" # Confirmed
Use consistent comments to ensure understanding alignment
Team Collaboration & Code Review
Establish unified enum naming standards for the team and ensure consistency through code reviews.
Team Naming Review Checklist
✓ Semantic Clarity
- • Do enum value names clearly express their meaning?
- • Are ambiguous or confusing names avoided?
- • Can new team members understand quickly?
- • Are extensive comments needed to understand? (If yes, naming is not clear enough)
✓ Consistency Check
- • Do values within the same enum use consistent naming patterns?
- • Is it consistent with the team's existing enums?
- • Are verb tenses unified (past, present, perfect)?
- • Are singular/plural forms reasonable and consistent?
✓ Technical Compatibility
- • Are language reserved words avoided?
- • Do enum value names conform to the language's naming conventions?
- • Are JSON serialization requirements considered?
- • Are there issues storing in databases? (length, character restrictions)
✓ Maintainability
- • Is it easy to maintain consistency when adding new enum values?
- • Is the ordering reasonable? (by importance, alphabetical, business flow)
- • Is there reserved space for expansion?
- • Is the impact scope of deleting or renaming enum values controllable?
Common Naming Pitfalls
Pitfall 1: Excessive Abbreviation
enum Status { PND, CONF, SHPD }
Hard to understand, should use complete words
Pitfall 2: Using Numeric Numbering
enum Priority { P1, P2, P3 }
Cannot express semantics, should use HIGH, MEDIUM, LOW
Pitfall 3: Including Type Names
enum OrderStatus {
ORDER_PENDING,
ORDER_CONFIRMED
}
Redundant, just use PENDING, CONFIRMED
Pitfall 4: Using Reserved Words
enum Action {
NEW, // Reserved word in some languages
CLASS, // Reserved word
RETURN // Reserved word
}
May cause compilation errors or confusion
Best Practice Recommendations
Practice 1: Use a Glossary
Team maintains a unified enum glossary to ensure similar concepts use the same vocabulary
Practice 2: Write Examples
Provide enum naming examples for various scenarios in team documentation
Practice 3: Use Linter
Configure code checking tools to automatically detect non-compliant naming
Practice 4: Regular Review
Regularly review existing enums to identify areas needing refactoring
Practice 5: Version Control
Use tools (such as Migration Planner) to manage enum changes
Real-World Case Studies
Learn how to apply naming strategies in different scenarios through real project cases.
Case 1: E-commerce Order System
Before Refactoring (Issues)
enum OrderStatus {
S1, // Unclear meaning
S2,
S3,
CANCEL, // Should use past tense
FINISH,
WAIT_PAY, // Inconsistent format
DELIVERING // Inconsistent tense
}
Issues: Using numbers, tense confusion, inconsistent format
After Refactoring (Improved)
enum OrderStatus {
PENDING_PAYMENT, // Pending payment
PAID, // Paid
PROCESSING, // Processing
SHIPPED, // Shipped
DELIVERED, // Delivered
COMPLETED, // Completed
CANCELLED, // Cancelled
REFUNDED // Refunded
}
Improvements: Clear semantics, unified format, sorted by business flow
Case 2: User Permission System
Before Refactoring (Issues)
enum Role {
ROLE_ADMIN, // Redundant prefix
NORMAL_USER, // Inconsistent format
GUEST_USER,
MOD, // Unclear abbreviation
SUPER // Incomplete
}
Issues: Redundant prefix, format confusion, inappropriate abbreviations
After Refactoring (Improved)
enum UserRole {
SUPER_ADMIN, // Super admin
ADMIN, // Admin
MODERATOR, // Moderator
USER, // User
GUEST // Guest
}
Improvements: Remove redundancy, complete naming, sorted by permission level
Case 3: Log Level System
Before Refactoring (Issues)
enum LogLevel {
LEVEL_1, // Does not express semantics
LEVEL_2,
LEVEL_3,
LEVEL_4,
LEVEL_5
}
Issue: Completely unable to express log level meaning
After Refactoring (Improved)
enum LogLevel {
FATAL, // Fatal error
ERROR, // Error
WARN, // Warning
INFO, // Info
DEBUG, // Debug
TRACE // Trace
}
Improvements: Use industry-standard naming, sorted by severity
Practical Tools & Resources
Use tools to help teams automatically check and maintain enum naming conventions.
Summary: Building an Excellent Enum Naming System
Establish Standards
Create team enum naming convention documentation, clearly defining naming patterns for various scenarios
Tool Support
Use Linter tools for automatic checking, configure CI processes to ensure convention enforcement
Continuous Improvement
Regular review and refactoring, continuously optimize naming conventions based on team feedback
Remember the three core principles: Readable, Predictable, and Portable. Good naming not only makes code clearer but also reduces communication costs, improves development efficiency, and lowers maintenance difficulty.