Skip to content

Consolidate Conditional: Merge Checks That Share a Result

Learn how to merge separate conditional checks that return the same result into one well-named condition, with TypeScript, Python and PHP examples.

6 min readRefactoring

Consolidate Conditional is a small refactoring. You take a run of separate checks that all lead to the same outcome and merge them into one boolean expression. Then you move that expression into a function whose name states the business rule. It matters in interviews because it shows you read code for intent, not just for syntax. Reviewers notice when you turn four scattered early returns into one rule like isFeeWaived. It tells them you can spot the concept the code is circling around and give it a name.

The problem

A situation where the obvious design starts to hurt.

Picture a coworking booking service called DeskHop. When a member cancels a meeting room, the service charges a late-cancellation fee of half the booking value. Over time, product added exceptions. Enterprise customers never pay. Cancelling 24 or more hours ahead is free. If DeskHop itself double-booked the room, the fee is waived as well.

Each exception arrived in a different sprint, so each got its own if that returns 0, and some got a comment explaining why. The method now reads as a list of unrelated guards. Nothing tells you they form a single rule: when the fee is waived. The next person who adds an exception has to scan every branch to work out where it belongs, and anyone asking "when do we waive fees?" has to rebuild the answer from those branches.

Before and after

The same code, before and after the change.

Before

type Plan = "basic" | "pro" | "enterprise";

interface Booking {
  plan: Plan;
  hoursBeforeStart: number;
  systemConflict: boolean;
  hourlyRateCents: number;
  durationHours: number;
}

function cancellationFeeCents(booking: Booking): number {
  // Enterprise contracts include free cancellation
  if (booking.plan === "enterprise") {
    return 0;
  }
  // Cancelling a day ahead is always free
  if (booking.hoursBeforeStart >= 24) {
    return 0;
  }
  // We double-booked the room, so it's on us
  if (booking.systemConflict) {
    return 0;
  }
  return Math.floor((booking.hourlyRateCents * booking.durationHours) / 2);
}

console.log(
  cancellationFeeCents({
    plan: "pro",
    hoursBeforeStart: 3,
    systemConflict: false,
    hourlyRateCents: 4500,
    durationHours: 3,
  })
); // 6750

After

type Plan = "basic" | "pro" | "enterprise";

interface Booking {
  plan: Plan;
  hoursBeforeStart: number;
  systemConflict: boolean;
  hourlyRateCents: number;
  durationHours: number;
}

function isFeeWaived(booking: Booking): boolean {
  return (
    booking.plan === "enterprise" ||
    booking.hoursBeforeStart >= 24 ||
    booking.systemConflict
  );
}

function cancellationFeeCents(booking: Booking): number {
  if (isFeeWaived(booking)) {
    return 0;
  }
  return Math.floor((booking.hourlyRateCents * booking.durationHours) / 2);
}

console.log(
  cancellationFeeCents({
    plan: "pro",
    hoursBeforeStart: 3,
    systemConflict: false,
    hourlyRateCents: 4500,
    durationHours: 3,
  })
); // 6750

Why it's worth it

What the change buys you.

The three guards were never independent. Together they answer one question: does this cancellation cost anything? Merging them with || makes that visible, and moving the expression into isFeeWaived gives the rule a name that product managers and support staff would recognise. The comments that explained each branch become redundant, because the function name and the conditions now carry that meaning.

Behaviour is unchanged. || in TypeScript, or in Python and || in PHP all short-circuit left to right, so the checks run in the same order as the original early returns and stop at the first true one. The fee calculation itself is untouched.

The payoff shows up at the next change. When legal asks for a fourth exemption, such as members with an open billing dispute, you add one line to isFeeWaived. You can also unit-test the waiver rule on its own, separately from the fee arithmetic. The same refactoring works for nested if statements that guard the same block. There you merge them with && instead of ||.

  • The business rule for waiving fees has one name and one location.
  • New exemptions are a one-line change instead of another scattered guard.
  • The waiver logic can be tested on its own, apart from the fee calculation.
  • The main function reads as a short summary: waived means free, otherwise charge half.

Careful with

  • Only merge checks that truly share a result. If two branches return the same value today for unrelated reasons that may diverge later, keep them separate.
  • Conditions with side effects cannot be merged safely, because short-circuiting may skip calls that previously ran.
  • Preserve the order of the checks. An expensive or failure-prone condition placed earlier can change performance or throw where the original did not.
  • If each branch logs or reports a different reason, consolidating loses that detail. Consider returning a reason value instead of a boolean.

Step by step

Small, safe steps, with the tests passing after each.

  1. Make sure cancellationFeeCents has tests covering each waiver branch and the charged case, and run them so you start green.

  2. Check that none of the conditions has side effects such as logging, mutation or I/O. If one does, separate that first or stop here.

  3. Merge the first two guards into a single if joined with ||, keeping their original order. Run the tests.

  4. Fold the third guard into the same expression with another ||. Run the tests.

  5. Extract the combined expression into a function named after the business rule, isFeeWaived, and call it from the if. Run the tests.

  6. Delete the comments that only restated each condition, and add direct tests for isFeeWaived. Run the full suite.

Smells it removes

Smells this refactoring helps remove.

Common questions

Short answers to the usual doubts.

When should I use && instead of || when consolidating?

Use || when several sequential checks each lead to the same early result, as with the waiver guards here. Use && when if statements are nested and only the innermost body runs, because every condition must hold for it to run. In both cases the merged expression should keep the original evaluation order.

Is this the same as Decompose Conditional?

They are related but different. Decompose Conditional takes one complicated condition and its branches and extracts them into named functions. Consolidate Conditional first merges several checks into one, then usually finishes by extracting that combined check, which is where the two overlap.

What if the combined expression becomes long and hard to read?

Split it inside the extracted function. Give sub-rules their own small predicates, such as cancelledEarly(booking), and combine those. The caller still sees one name, and each piece stays readable.

How would I present this refactoring in a coding interview?

Point out that the branches return the same value, state the rule they express in plain words, confirm there are no side effects, and then merge and name it. Mention that short-circuit evaluation preserves the original order, which shows you checked that behaviour is preserved.

Next guide3 of 5 · Techniques: simplifying conditionalsReplace Nested Conditional with Guard ClausesRead the next one