# 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.

## Overriding Next.js Link and Image Components

Next.js ships with these two incredibly useful utility components. You can further abstract these to clean up your code.

Feb 09, 2023

## Export ES6 Class Globally with webpack

webpack creates its own scope for your bundle, but you can make it globally available.

Dec 01, 2018

## Get a Snapshot of Mac OS X Shell Commands

You can get a snapshot of the progress of terminal/shell jobs on Mac OS X by hitting Ctrl+T.

Dec 05, 2014