Tom Ritchford
Jan 21, 2023

--

Early returns are potentially dangerous, so you can't just sprinkle them around like candy.

For example, in function or method that's more than a few lines, it's easy to miss the early return, and believe that later code is being executed.

Many people goes so far as to espouse the "single return principle" that says that there can only be one way to leave a function, because it does prevent a few classes of error entirely.

I still think that's too much. I agree with early returns for errors or trivial cases for example, as in your code.

Early returns can make the code significantly clearer and more readable, which means it is more likely to be correct.

Still, I think it's also important to teach it to people with a little warning.

Good article in general.

--

--

Responses (1)