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

What You Need to Know When Using Netlify Forms

Netlify forms are an incredibly powerful feature. They enable you to accept dynamically-driven user data on your static site. But you must understand a few key concepts about Netlify forms if you're going to have success working with them.

May 02, 2019

Pro Tip: Use “1.” for Every Ordered List Item when Writing Markdown

A quick tip for making ordered lists in markdown easier to write.

May 31, 2022

The Spinneret: Issue #11

A month of transitioning into a new job and working to wrap a side project so I can get back to focusing on this newsletter and website.

Aug 22, 2021