Please stop using nested if statements.

Remove the nesting and use ternaries instead.

Image of code on a notebook screen.
Photo by Marc Mueller on Unsplash

It is harder to read code than to write it.
— Joel Spolsky

I can’t tell you how many times I have run across something like this:

While this is a simple example, let’s rewrite this and take out the nested if statements:

That’s better. Now, we can get a better understanding of when each function call is returned, but I think we can easily do better. Let’s try and turn this into a ternary statement and keep the nesting out:

If you don’t agree with me, that is okay. Everyone is allowed to write code how they see fit and this is absolutely NOT a one-size-fits-all and won’t work in every situation. I would love to hear your thoughts!

--

--

--

Senior Software Engineer @ Olo - Any views or opinions expressed here are my own and are not a reflection of my employer or anyone else.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Keep calm and git bisect

Plug Talk — An Open Source Community Chat App

‘Coding’? Lekin yeh kya hai?

Codeforces Hello 2022 Overview

OOPs Concept in Python

6 steps to connect C6807-XL with Cisco 6800ia switches

Workout #20: Word Count

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shane Tarleton

Shane Tarleton

Senior Software Engineer @ Olo - Any views or opinions expressed here are my own and are not a reflection of my employer or anyone else.

More from Medium

Efficient set operations on arrays of objects in JavaScript

Use These FP Techniques to Create Better Loops in JavaScript

An introduction to type programming in TypeScript — zhenghao

Typescript Loop in Certain Time ⌚