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];

Explanation

Data-driven mapping is shorter and easier to extend. When each case has different behavior, objects with a shared method (polymorphism) fit better.

Further reading

Problem with a question

Design Patterns · Junior · question 55

Test yourself

Take the Design Patterns · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz