Skip to content

Remove Flag Argument: Replace a Boolean with Explicit Functions

Learn to replace a boolean parameter that switches a function's behaviour with two clearly named functions, in safe steps with tests passing throughout.

7 min readRefactoring

A flag argument is a parameter, usually a boolean, that the caller passes to pick which branch of a function runs. The function signature looks tidy, but every call site reads like issueRefund(order, true), and nobody can tell what true means without opening the implementation. Remove Flag Argument splits that function into one explicit function per behaviour, so each caller states its intent by name. This guide walks through the refactoring on an invented ebook store called Pagebound, with the same code in TypeScript, Python and PHP. Interviewers like this one because it is small, easy to get wrong, and shows whether you can tell a real parameter from a hidden mode switch.

The problem

A situation where the obvious design starts to hurt.

Pagebound refunds orders in two ways. Support agents give customers store credit with a 10 percent bonus, credited to the customer's wallet. Automatic cancellations, such as when a publisher pulls a title, send the full amount back to the original payment method.

Both paths live in one function, issueRefund(order, asStoreCredit). The callers pass literal true or false, so the call sites say nothing about what happens. The two branches share almost no logic, yet they are bound together. Anyone adding a third refund type is pushed towards a second boolean, and two booleans give you four combinations, some of which make no sense.

Before and after

The same code, before and after the change.

Before

interface Order {
  id: string;
  totalCents: number;
  paymentId: string;
  customerId: string;
}

interface Refund {
  orderId: string;
  method: "store_credit" | "original_payment";
  amountCents: number;
  destination: string;
}

export function issueRefund(order: Order, asStoreCredit: boolean): Refund {
  if (asStoreCredit) {
    const bonus = Math.floor(order.totalCents / 10);
    return {
      orderId: order.id,
      method: "store_credit",
      amountCents: order.totalCents + bonus,
      destination: "wallet:" + order.customerId,
    };
  }
  return {
    orderId: order.id,
    method: "original_payment",
    amountCents: order.totalCents,
    destination: order.paymentId,
  };
}

export function handleSupportTicket(order: Order): Refund {
  return issueRefund(order, true);
}

export function handleAutoCancellation(order: Order): Refund {
  return issueRefund(order, false);
}

const order: Order = { id: "PB-1042", totalCents: 1899, paymentId: "card_7731", customerId: "cu_58" };
console.log(handleSupportTicket(order));
console.log(handleAutoCancellation(order));

After

interface Order {
  id: string;
  totalCents: number;
  paymentId: string;
  customerId: string;
}

interface Refund {
  orderId: string;
  method: "store_credit" | "original_payment";
  amountCents: number;
  destination: string;
}

export function refundAsStoreCredit(order: Order): Refund {
  const bonus = Math.floor(order.totalCents / 10);
  return {
    orderId: order.id,
    method: "store_credit",
    amountCents: order.totalCents + bonus,
    destination: "wallet:" + order.customerId,
  };
}

export function refundToOriginalPayment(order: Order): Refund {
  return {
    orderId: order.id,
    method: "original_payment",
    amountCents: order.totalCents,
    destination: order.paymentId,
  };
}

export function handleSupportTicket(order: Order): Refund {
  return refundAsStoreCredit(order);
}

export function handleAutoCancellation(order: Order): Refund {
  return refundToOriginalPayment(order);
}

const order: Order = { id: "PB-1042", totalCents: 1899, paymentId: "card_7731", customerId: "cu_58" };
console.log(handleSupportTicket(order));
console.log(handleAutoCancellation(order));

Why it's worth it

What the change buys you.

A boolean that only ever arrives as a literal is not data, it is a choice of function made at the call site and then hidden inside a parameter. Making that choice visible as a function name costs nothing and removes a whole class of misreadings: refundAsStoreCredit(order) cannot be confused with its sibling, while issueRefund(order, true) invites someone to guess.

Splitting the function also lets each behaviour evolve alone. If finance later decides that card refunds should deduct a payment processor fee, that change lands in refundToOriginalPayment and cannot leak into the store credit path through a misplaced condition. Each function also gets a shorter, branch-free body that is simpler to test, since each test targets one behaviour instead of one combination of inputs.

Finally, the API becomes discoverable. Autocomplete shows two named options instead of one function with a mystery switch, and adding a third refund type later means adding a third function rather than a second boolean.

  • Call sites state their intent by name, so readers no longer need to open the implementation to decode true or false.
  • Each behaviour lives in its own short function that can change without risk to the other branch.
  • Tests target one behaviour per function instead of enumerating flag combinations.
  • New variants are added as new functions rather than extra booleans that multiply the number of paths.

Careful with

  • If some callers compute the flag from runtime data, such as a customer's choice on a form, do not force them to duplicate the if statement. Keep a thin dispatcher that picks one of the explicit functions, or pass an enum instead of a boolean.
  • When the two branches share most of their logic, extract the shared part into a private helper first, otherwise splitting the function creates duplicated code.
  • If the flagged function is part of a public API or library, keep the old signature as a deprecated wrapper around the new functions until external callers have migrated.
  • Watch for dynamic or reflective calls that a text search will miss, such as handlers registered by name in configuration.

Step by step

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

  1. Make sure tests cover both branches: one call with the flag set to true and one with it set to false, asserting the full returned Refund for each.

  2. Create refundAsStoreCredit(order) whose body is just return issueRefund(order, true). Run the tests.

  3. Create refundToOriginalPayment(order) whose body is just return issueRefund(order, false). Run the tests.

  4. Change each caller that passes a literal flag to call the matching new function, one call site at a time, running the tests after each change.

  5. Search the codebase for any remaining calls to issueRefund. If none remain, copy the store credit branch into refundAsStoreCredit and the original payment branch into refundToOriginalPayment, replacing the delegating calls. Run the tests.

  6. Delete issueRefund and run the tests and the type checker or linter to confirm nothing still references it.

Smells it removes

Smells this refactoring helps remove.

Common questions

Short answers to the usual doubts.

What if the boolean comes from user input rather than a literal?

Then the flag carries real data and the decision has to happen somewhere. Push it to the edge: the form handler reads the customer's choice and calls refundAsStoreCredit or refundToOriginalPayment. If many callers need the same decision, a small dispatcher function or a RefundMethod enum parameter is clearer than a bare boolean.

Is an enum parameter just as good as separate functions?

An enum fixes readability at the call site, since issueRefund(order, RefundMethod.StoreCredit) is self-explanatory. Separate functions go further by keeping the bodies independent. Prefer separate functions when callers always know the mode statically, and an enum when the mode genuinely flows in as data.

How do I spot a flag argument in an interview code review?

Look for a boolean parameter whose main use is an if statement near the top of the function that selects between unrelated blocks, and call sites passing true or false literals, often with a comment explaining what the literal means. A boolean that feeds a calculation, such as includeTax in a price formula used in one expression, is a weaker case.

Does this refactoring change behaviour or performance?

No. Each new function contains exactly the code from one branch, so the outputs are identical, which the tests from the first step confirm. Runtime cost is the same or marginally lower because one conditional check disappears.

What about functions with two or three boolean flags?

Those are worse, because n booleans allow 2 to the power n combinations and some are usually invalid. Find out which combinations callers actually use, give each a named function, and check whether the remaining combinations should be rejected entirely.

Next guide4 of 4 · Techniques: simplifying callsSeparate Query from Modifier: Split Reads from State ChangesRead the next one