Home

Cleaning Up Nested Conditionals

Flattening the logic of if/else conditionals can go a long way toward cleaning up your code.

(The examples in this article are written in JavaScript, but the principles can be applied to any language.)


We've all done (and may still do) something like this:

var myFunction = function (a, b, c) {
if (a > 0) {
if (b > 0) {
if (c > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
} else if (a == 0) {
if (b == 0) {
if (c == 0) {
return 0;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
};

Just look at that 25-line mess!

Are you able to cut through the nastiness to see what this is actually doing? It's really quite simple. The function myFunction takes three arguments, and:

  • If all three arguments are positive, it returns true.
  • If all three arguments are zero, it returns zero.
  • Otherwise, it returns false.

That natural next step one may take when refactoring this code is to combine conditionals where possible. That would leave something like this:

var myFunction = function (a, b, c) {
if (a > 0 && b > 0 && c > 0) {
return true;
} else if (a == 0 && b == 0 && c == 0) {
return 0;
} else {
return false;
}
};

Phew! From 25 to 9 lines. This at least reads a bit more semantically.

But what if I told you I still thought this was messy ...

If we introduce exit conditions where applicable, we can continue through the flow of the function without being nested within an if/else conditional statement. An exit condition requires the program (in this case, the function) to cease (or return) before getting through all its code.

Taking this approach, the function can be adjusted like so:

var myFunction = function (a, b, c) {
if (a > 0 && b > 0 && c > 0) return true;
if (a == 0 && b == 0 && c == 0) return 0;
return false;
};

By using exit conditions, we've effectively flattened the logic within this function. That may not seem like much in this scenario, but if you needed to add some additional behavior after checking whether all the values were positive or zero, this would really come in handy.

So, next time the first line in a function you write opens an if/else conditional, stop yourself. Figure out how to introduce exit clauses to clean up your code.


Pro Tip: One more thing before you leave. Exit clauses should occur as early in the function as possible. Your function shouldn't have to process any unnecessary logic if it meets one of the exit conditions.

Let's Connect

Keep Reading

How I Organize My Brain

There are so many tools claiming to be the next best thing for staying organized and productive. I've formed a cocktail from my favorite tools to keep my brain (my life) organized.

Feb 13, 2021

How to Use PNPM in Netlify Build

PNPM is now natively supported by Netlify.

Mar 19, 2021

3 Methods for Logging Output During Ruby Processes

Logging output during ruby processes is hugely beneficial for gaining insight into running code.

Jul 13, 2018