Thank you for stopping by! I am a full-stack developer that combines the power of entrepreneurship and programming to make the lives of programmers easier.
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.
The first option is best here for 3 reasons:
optionResult
is actually aswitch
orif else
arg
has an unexpected value, whereas in the second version you can't do that unless you check ifoptionResult
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.Thanks for the detailed response,
I agree that in the code above I haven't considered doing some validation.