Re:Using ‘switch’ to match arbitrary logic instead of string...
by hackinthebochs on Monday August 19, 2019 @12:18PM
(#20739102)
>avoid early returns because they can be confusing
It seems to me like the exact opposite is true. Early return conditions stand out in your code and so the structure matches the flow. Its much clearer than having error state weaved through the mainline behavior of the function.
I'm pretty sure the pattern of having only one return is popular among older engineers because it makes it easier to use pre-condition/post-condition/invariant style reasoning which used to be popular. Outside of this scenario I see no reason to avoid early returns.
I kind of see what he's talking about. It seems like it's a little easier to reason with the code and easier to set breakpoints sometimes.
All that's to say: I'm curious as to why early returns would be better in JS.