DEV Community

Cover image for Why we shouldn't use "else"

Why we shouldn't use "else"

David Díaz on January 03, 2021

I have recently watched several videos and read many posts about the misuse of 'else' and why we should avoid it. At first I thought 'well, it will...
Collapse
 
phlash profile image
Phil Ashby

I generally follow a similar pattern, which is also known as 'early return' and nicely explained here by Szymon: szymonkrajewski.pl/why-should-you-...

What matters to me is that there is a common pattern to functions across a codebase, so the reader (hello you in 15 months time) can see the 'happy path' easily, and can see the priority of unhappy conditions, which communicates what matters more too.

Use of assertions or guard conditions also results in a similar pattern for good reason :)

Collapse
 
scott_yeatts profile image
Scott Yeatts

I once argued AGAINST early return, but then I tried it and I can't imagine why I didn't use it before.

Early on in my career (a long time ago in a galaxy far far away and all that) I had been taught a rule that a function should only ever have one return in order to make it clear what was being returned, and I clung to that rule for too long.

It's one of the best changes I've made to my code style in years. I love it.

Collapse
 
patricktingen profile image
Patrick Tingen • Edited

I was taught exactly the same thing, but when you adhere to that you end up with massive blocks. Early return is king!

Collapse
 
jamesthomson profile image
James Thomson

I've been using this method for years now and can definitely say it makes code far more legible and concise. If/else has it's place at times, but I really think (and hope) that early returns should be taught as a standard practice.

Collapse
 
stevetaylor profile image
Steve Taylor

I wouldn’t religiously avoid else just because some people abuse it. That would make a lot of code significantly less expressive and result in unnecessary branch condition evaluations. A simple if-else, without else, would have two ifs evaluating the same expression, with the 2nd expression negated.

Collapse
 
sroehrl profile image
neoan • Edited

The early return pattern helps you to write clean, performant code. And your example is ideal. However, there are of course cases where else is the best solution, so your title threw me off a little.

Collapse
 
dd8888 profile image
David Díaz

Agree with you, maybe I should add "sometimes" 😅

Collapse
 
realtebo profile image
Mirko Tebaldi

I also prefer to fail fast. So i do this checks in a function and return at first failed checked. So no else.

Anyway, else is not evil and can be very clear to force programmer mind to understand that two condition are not mutually exclusive. It helps months ago I wrote something

Collapse
 
bonespiked profile image
bonespiked

early return is good - but as a nitpicky thing, verification stuff should be done together so you can inform the user of all the errors at once, rather than hitting them with a stick, one error at a time....

Collapse
 
rolfstreefkerk profile image
Rolf Streefkerk

This particular example is misuse of else, there can be general cases that you need to cover with the else that would not need separate if statements.

In short, making general statements that else should be avoided is incorrect. It depends largely on context

Collapse
 
kalashin1 profile image
Kinanee Samson

I use the else keyword most time, I'll take note tho