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

My Favorite Tool for Managing Project-Specific Environment Variables

With many local projects, environment variables often conflict with one another. I tried several tools before landing on my favorite for managing project-specific values.

Jan 15, 2019

Using Notion as a Publishing Workflow

To minimize friction in publishing new blog content, I went through an experiment that used Notion as a publishing engine. After three months, I’m posting at a rate faster than ever before.

Aug 03, 2022

Build Jamstack Sites Faster with Conventional Tooling

The Jamstack presents the opportunity to fly through the process of building a website, but it helps if you build a foundation on which you can do work consistently.

Aug 05, 2020