Return early
One of my pet hates in JavaScript is code nested inside deep if
s and else
s. It seems to make functions a lot harder to follow, and nine times out of ten can be avoided. I’ll try to demonstrate by showing a few before and after examples.
Nested if else
Could be re-written like
Large if blocks
It is common that we need to address two different code paths in one function. I often see code where the the majority of the function is nested inside an if
or else
block. By the time we have got to the else
, we have forgotten what the condition was in the first place.
By returning early we can address the edge cases and anomalies before we get to the core logic. Every time we return, we are starting from a clean slate. The reader no longer has to hold all that state in their memory…relief!
Conclusion
The best type of code we can write is code that others’ can quickly understand and contribute to. I’m always looking for ways to make my logic as simple to follow as possible.
Each nested block demands an allocation of short-term memory from the reader’s brain. As the reader dives deeper and deeper into nested blocks they will eventually blow their capacity, and lose their train of thought.
If we avoid nesting, we make our code easier to read. If code is easier to read, more people can contribute, and bugs can be more easily spotted.
I’m looking to add more examples of how returning early can simplify logic, if you have any, please say so and I will add them to this post.