DEV Community

Discussion on: Which would you prefer and why?

Collapse
 
codevault profile image
Sergiu Mureşan • Edited

The first option is best here for 3 reasons:

  • It has better performance (only processes the option it lands on, not all of them)
  • More intuitive in the long run (if the function becomes much larger then it is hard to understand that the object optionResult is actually a switch or if else
  • You are returning an empty string if arg has an unexpected value, whereas in the second version you can't do that unless you check if optionResult doesn't have the property which further adds to the complexity of the function.

You can also straight up return the optionResult inside the if statements if no post-processing is needed.

Collapse
 
lozadaomr profile image
Omar Lozada

Thanks for the detailed response,
I agree that in the code above I haven't considered doing some validation.