News for hackers. Stuff that matters.
Re:Using ‘switch’ to match arbitrary logic instead of string... by falcrist on Monday August 19, 2019 @07:41AM (#20736876)
I typically code in c and c#, and have been told by older engineers that it's important to avoid early returns because they can be confusing, especially when skimming through the code.

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.

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.