Good programming practices - Early Return — Lesson 1

In this series, I’ll try to keep smart practices for beautiful coding. Since coding is a craftsmanship, therefore bring your humanity to your art.

Danish Siddiq
Mar 5, 2021
Ok then keep your secrets :D

Return Early:

I will try to keep examples short and simple:

Complicated Code:

Early Return Version:

Code Analysis:

Bouncer Pattern:

Bouncer pattern reduces code by holding validation logic. It is helpful specially when code extensively uses nested if-else statements and reduces complexity. Bouncer pattern comprehends early return to simplify code.

Conclusion:

Although one can argue that the same is achievable by writing code meticulously in a conventional manner, I personally agree that early return makes the code neat, readable and maintainable. Let’s move to the next tip ; )

“And He found you lost and guided [You]” — Ad Duhaa

--

--