DEV Community

Discussion on: Why I prefer objects over switch statements

Collapse
 
itr13 profile image
Mikael Klages

It's kind of interesting that the way you use objects here allow you to write code similar to how switch statements work in some languages.

As an added bonus, writing it like this may let you implement a different feature present in some languages, that checks if a switch has covered all possible inputs (usually used with enums). This could prevent stuff like, having one entry misspelled, or somebody adding something somewhere, but not updating all the linked "switch"es.

Though depending on the implementation, this may be even more work or cause more trouble.