Should an Enum start with a 0 or a 1?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Whether enum values should start at zero or one depends on domain semantics and integration constraints, not style preference alone. Many codebases start at zero because runtimes default enum storage to zero, but external protocols often require one-based codes. The important rule is to assign explicit values and keep them stable.
Why Zero Is Often the Best First Value
Zero is commonly used as a safe sentinel such as Unknown, None, or Unspecified. This is defensive because default-initialized enum fields become zero automatically.
With this design, accidental default values do not pretend to be valid business states.
When Starting at One Is Reasonable
One-based enums make sense when external systems already define codes that way, or when zero is reserved by contract.
Business values start at one, while zero remains an explicit sentinel.
Treat Numeric Values as Compatibility Contract
If enums are persisted in databases, serialized in APIs, or exchanged with other services, numeric values become part of a public contract. Reordering members without explicit values can silently break compatibility.
Always assign explicit numbers for persisted or external enums.
Validate Raw Integers Before Casting
Casting arbitrary integers to enum types can produce invalid states. Validate values at boundaries.
This pattern is useful for API inputs and database reads.
Flags Enums Follow Different Rules
Bitwise flags enums are not ordinal categories. Use powers of two and keep zero as None.
Do not use sequential numbers for flags semantics.
Separate Domain Codes From UI Ordering
UI order often changes for usability reasons. Do not encode display ordering in enum numeric values.
Keep numeric values stable for contracts and use separate mapping for presentation.
Add Stability Tests
Add tests to detect accidental enum value changes during refactors.
These tests are low cost and high value in API-driven systems.
API Serialization Guidance
If your API exposes enum values, decide whether to serialize names or integers. Name serialization is more readable, but integer serialization may be required by existing clients. Either way, document mapping clearly and keep backward compatibility.
For example, if integer codes are public, include enum table in API docs and changelog.
Decision Framework
A practical framework:
- if default value should mean unknown, start with zero sentinel
- if external contract is one-based, mirror contract and reserve zero explicitly
- assign explicit numbers always
- validate integer inputs at boundaries
- add tests for value stability
This keeps behavior predictable across code evolution.
Common Pitfalls
A common pitfall is letting zero map to a valid state unintentionally because of default initialization. Another is relying on implicit enum numbering and then reordering members later. Teams often cast untrusted integers directly to enums without validation. Flags enums are also frequently assigned sequential values incorrectly. UI display order is sometimes tied to numeric enum codes, making presentation changes risky.
Summary
- Zero is a strong default when used as explicit
UnknownorNone. - One-based enum values are valid when required by external contracts.
- Assign explicit numeric values for every enum member.
- Validate raw integer inputs before converting to enums.
- Use powers of two for flags enums and zero for
None. - Keep enum contract values separate from presentation ordering logic.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.