TLDR; Keep nesting low, avoid using else branches, have fun reading code instead of trying to understand it.
“A nest is a structure built by animal(s) to hold its eggs, its offspring, or occasionally the animal itself. Although nests are most closely associated with birds, members of all classes of vertebrates and some invertebrates construct nests. They may be composed of organic material such as twigs, grass, and leaves, or may be a simple depression in the ground, or a hole in a rock, tree, or building. Human-made materials, such as string, plastic, cloth, or paper, may also be used. Nests can be found in all types of habitat.”
Nested code has become a pet peeve of mine in my first few years of being a dev. It’s only after you have to dive into someone else’s mess or decode your own ingeniously complex black magic a week after you wrote it, that you start to wonder if it’s a good idea to build little pyramids in your code.

At first, wrapping your little code egg in an if statement to perform some validation looks like a good idea. A few business rules later and your code is so nested, not even the flying spaghetti monster can help you untangle it.
Before we get into too much details, let’s look at an example and how we can get rid of unnecessary nesting. Below is an example of some nested code that performs some validation and if successful, subscribes a user to some service.
/** * Super bad nesting */ if (user != undefined) { if (user.username != undefined) { if (user.password != undefined) { if (user.login()) { if (user.accountBalance >= 500) { var success = service.subscribe(user, '500k'); if (success) { toastr.success('User has been subscribed'); } else { throw 'User subscription failed'; } } else { throw 'user has insufficient funds' } } else { throw 'user not logged in' } } else { throw 'user password undefined'; } } else { throw 'username not defined'; } } else { throw 'user not defined'; }
Now following only one simple technique we can eliminate deep nesting completely. The following code snippet shows how we inverted the conditional checks in order to fail fast and reduce nesting.
/** * No nesting at all, cool! */ if (user == undefined) { throw 'user not defined'; } if (user.username == undefined) { throw 'username not defined'; } if (user.password == undefined) { throw 'user not defined'; } if (!user.login()) { throw 'user not logged in' } if (user.accountBalance < 500) { throw 'user has insufficient funds' } var success = service.subscribe(user, '500k'); if (!success) { throw 'User subscription failed'; } toastr.success('User has been subscribed');
Now this code is so much easier to read. Instead of jumping between the if and the else, we can see immediately see what the code is trying to do and what will happen on validation failures.
This minor refactor opens up even more possibilities to clean up the code. We can now start to extract some of the validation checks into separate methods and simplify our implementation.
/** * Main method to subscribe a user to a service */ try { // get user context user.login(); service.subscribe(user, '500k'); toastr.success('User has been subscribed'); } catch (e) { // handle exception here } user.login = function () { if (user.username == undefined) { throw 'username not defined'; } if (user.password == undefined) { throw 'user password undefined'; } // login logic here }; service.subscribe = function (user, product) { var productPrice = service.getProductPrice(product); if (user.accountBalance < productPrice) { throw 'User has insufficient funds' } // subscription logic here };
So, to summarise
- Think before you code
- Refactor your code
- Factor complex logic operations into methods to aid readability, re-usability, maintainability.
- Fail fast
- Localise your code
- Use shorthand if for SIMPLE operations
- Avoid using else statements