Remove Unnecessary `Else` Blocks with Guards and Early Returns

Share this video with your friends

Send Tweet

Conditional logic can be very cumbersome and difficult to read. A single if/else block might be simple enough, but nesting several of them can lead to very complicated logic and difficult to read code.

Often, we can easily eliminate half of the branching through the use of guard statements and early returns. This lesson will take a function with a few nested if/else blocks and refactor it to remove all of the elses. The result will be simpler and, in my opinion, easier to read.