DEV Community

Discussion on: Use an object instead of a switch

Collapse
 
marpme profile image
marpme (Marvin)

I think I tend to see your use-cases here, but apperately this might have been a bad choice for replacing it with a map methodology.

  1. Switch-Case are generally understood even by all kind of software engineers (while loose object referencing is not that common in many languages)
  2. the switch could be refactored to make it less complex:
const getMonthTranslationKey = (month: Month) => {
  switch(month) {
    case Month.JANUARY: 
      return "JANUARY_KEY"
    case Month.FEBRUARY:
      // ...
  }
}
Enter fullscreen mode Exit fullscreen mode

This example would definitely save you some congnitive effort understanding this piece of code by removing the variable and the late return statement.