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

The Evolution and Redefinition of The Jamstack

The Jamstack was a revolution. Which led to an evolution. Now it's time to look at what changed and decide what Jamstack means today.

May 27, 2021

Simplify Context Switching with Browser Tab Groups

Browser tab groups can help contextualize tabs to make it easier to jump back into a project after you’ve been pulled away.

Dec 02, 2022

WTF is a Headless CMS

The headless CMS is a core tenet of the Jamstack approach, a gamechanging approach to building modern websites. Here is an intro to the headless CMS approach.

Mar 26, 2020