Enums
An enumeration names a closed set of domain choices. Validate external values before treating them as members; a label alone does not enforce legal state transitions.
What you’ll understand
- An enumeration names a closed set of domain choices. Validate external values before treating them as members; a label alone does not enforce legal state transitions.
- Normalize at the boundary by checking membership in the finite OrderStatus vocabulary.
- Evaluate the tradeoff: A closed vocabulary makes exhaustive decisions easier but must evolve with the domain. Python Enum supplies identity and membership; JavaScript needs explicit validation even with a frozen constant object.
Useful first: Classes and Objects
The idea and when to recognize it
An enumeration names a closed set of domain choices. Validate external values before treating them as members; a label alone does not enforce legal state transitions.
- Requirement: An order API receives a status string. Downstream code should receive one of three known values or an explicit rejection.
- Change to anticipate: Prevent unknown states from being treated as valid orders while keeping all legitimate values available.
- Design goal: Normalize at the boundary by checking membership in the finite OrderStatus vocabulary.
Start with the concrete problem
An order API receives a status string. Downstream code should receive one of three known values or an explicit rejection.
Prevent unknown states from being treated as valid orders while keeping all legitimate values available.
| Direct implementation | Revised design |
|---|---|
| Accepting any string lets misspellings become new states and bypass later business rules. | Normalize at the boundary by checking membership in the finite OrderStatus vocabulary. |
| The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design. | A closed vocabulary makes exhaustive decisions easier but must evolve with the domain. Python Enum supplies identity and membership; JavaScript needs explicit validation even with a frozen constant object. |
Responsibilities and contracts
Normalize at the boundary by checking membership in the finite OrderStatus vocabulary.
- OrderStatus: Defines the accepted vocabulary. Public operations: NEW, PAID, SHIPPED
- Parser: Rejects unknown external values. Public operations: parse(value)
Read the design
Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.
Preparing the design walkthrough…
Follow the interaction
Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.
Preparing the design walkthrough…
Implementation and expected output
Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.
Preparing the design walkthrough…
Try the idea in another setting
A task scheduler can validate a fixed priority vocabulary before selecting its queue.
- Name the new client contract before adapting the example.
- Identify which state belongs to each object and who controls its lifetime.
- Write one normal case and one boundary case before extending the design.
Tradeoffs and interview takeaways
A closed vocabulary makes exhaustive decisions easier but must evolve with the domain. Python Enum supplies identity and membership; JavaScript needs explicit validation even with a frozen constant object.
Common mistake: Accepting any string lets misspellings become new states and bypass later business rules.
- Explain the choice: Normalize at the boundary by checking membership in the finite OrderStatus vocabulary.
- Challenge your choice: A closed vocabulary makes exhaustive decisions easier but must evolve with the domain. Python Enum supplies identity and membership; JavaScript needs explicit validation even with a frozen constant object.
- Describe a requirement that would make the simpler implementation sufficient.
Design challenge and knowledge check
Preparing the design walkthrough…