Simplifying Your If Statements By Extracting Complex Conditions

July 04, 2022

Most of the time, we can make simple changes to increase the readability of our if statements

For example:


if (!(id?.number <= highestNumber)) {
    doSomething()
}

// we can rewrite this as 

if (id?.number > highestNumber) {
  doSomething();
}

However, sometimes we can run into if statements that are a bit tough to read through:


if (
  (user?.id?.type === "admin" || user?.id?.type === "manager") ||
  (cartContext[cartId]?.pastOrders?.length > 0 &&
    !!checkout?.id &&
    cartContext[cartId].currentOrder[checkout?.id] &&
    cartContext[cartId].currentOrder[checkout?.id].status?.isPaid === true &&
    (cartContext[cartId].currentOrder[checkout?.id]?.orderTotal >= 500 ||
      (user?.isBigSpender && orderValue >= 200) ||  
      ["USA", "Japan"].includes(user?.location)
    )
  )
) {
  doSomething();
}

Let’s extract some of that logic out of of the if statement


const HIGH_SPENDING_AREAS = ["USA", "Japan"]

const isAdminOrManager = user?.id?.type === "admin" || user?.id?.type === "manager";

const hasPastOrder = cartContext[cartId]?.pastOrders?.length > 0;

const currentOrderIsValidAndPaid = !!checkout?.id && cartContext[cartId]?.currentOrder[checkout?.id]?.status?.isPaid === true;

const userIsFromHighSpendingArea = HIGH_SPENDING_AREAS.includes(user?.location);

const isHighValueCustomer =
  cartContext[cartId]?.currentOrder[checkout?.id]?.orderTotal >= 500 ||
  (user?.isBigSpender && orderValue >= 200) ||
  (userIsFromHighSpendingArea)
  

if (isAdminOrManager || (hasPastOrder && currentOrderIsValidAndPaid && isHighValueCustomer)) {
  doSomething();
}

Benefits of Extracting Complex Conditions

1. Readability

Extracting complex conditions makes your code easier to read and understand.

2. Reusability

Once you’ve extracted complex conditions, you can reuse them in other parts of your codebase.

3. Testability

Extracted conditions can be independently tested, allowing you to validate their correctness in isolation.

4. Debugging

When debugging your code, having separate variables for extracted conditions allows you to inspect and monitor their values independently.

Conclusion

In the moment, it can be easy to just add another condition to an already long if statement in your codebase.

For the sake of your future self and others, let’s try to make our code more readable..

Did you find this blog post helpful?

Feel free to contact me using my website.

Have a fantastic day!


Profile picture

Learn about his journey from English teacher to software engineer.

Made by Zach Stone