Design Patterns · Junior · Question 55 of 75
How can a long switch that maps a status to a label be simplified?
const labels = { draft: 'Draft', published: 'Published', archived: 'Archived' };
const label = labels[status];- ✓Correct answer: Replace it with a lookup object (or polymorphism when behavior differs)
- 2Wrap it in a
try/catch - 3Split it into several
ifstatements
Explanation
Data-driven mapping is shorter and easier to extend. When each case has different behavior, objects with a shared method (polymorphism) fit better.