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));from dataclasses import dataclass
@dataclass(frozen=True)
class Order:
id: str
total_cents: int
payment_id: str
customer_id: str
@dataclass(frozen=True)
class Refund:
order_id: str
method: str
amount_cents: int
destination: str
def issue_refund(order: Order, as_store_credit: bool) -> Refund:
if as_store_credit:
bonus = order.total_cents // 10
return Refund(
order_id=order.id,
method="store_credit",
amount_cents=order.total_cents + bonus,
destination=f"wallet:{order.customer_id}",
)
return Refund(
order_id=order.id,
method="original_payment",
amount_cents=order.total_cents,
destination=order.payment_id,
)
def handle_support_ticket(order: Order) -> Refund:
return issue_refund(order, True)
def handle_auto_cancellation(order: Order) -> Refund:
return issue_refund(order, False)
if __name__ == "__main__":
order = Order("PB-1042", 1899, "card_7731", "cu_58")
print(handle_support_ticket(order))
print(handle_auto_cancellation(order))<?php
final class Order
{
public function __construct(
public readonly string $id,
public readonly int $totalCents,
public readonly string $paymentId,
public readonly string $customerId,
) {}
}
final class Refund
{
public function __construct(
public readonly string $orderId,
public readonly string $method,
public readonly int $amountCents,
public readonly string $destination,
) {}
}
function issueRefund(Order $order, bool $asStoreCredit): Refund
{
if ($asStoreCredit) {
$bonus = intdiv($order->totalCents, 10);
return new Refund(
$order->id,
'store_credit',
$order->totalCents + $bonus,
'wallet:' . $order->customerId,
);
}
return new Refund(
$order->id,
'original_payment',
$order->totalCents,
$order->paymentId,
);
}
function handleSupportTicket(Order $order): Refund
{
return issueRefund($order, true);
}
function handleAutoCancellation(Order $order): Refund
{
return issueRefund($order, false);
}
$order = new Order('PB-1042', 1899, 'card_7731', 'cu_58');
var_dump(handleSupportTicket($order));
var_dump(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));from dataclasses import dataclass
@dataclass(frozen=True)
class Order:
id: str
total_cents: int
payment_id: str
customer_id: str
@dataclass(frozen=True)
class Refund:
order_id: str
method: str
amount_cents: int
destination: str
def refund_as_store_credit(order: Order) -> Refund:
bonus = order.total_cents // 10
return Refund(
order_id=order.id,
method="store_credit",
amount_cents=order.total_cents + bonus,
destination=f"wallet:{order.customer_id}",
)
def refund_to_original_payment(order: Order) -> Refund:
return Refund(
order_id=order.id,
method="original_payment",
amount_cents=order.total_cents,
destination=order.payment_id,
)
def handle_support_ticket(order: Order) -> Refund:
return refund_as_store_credit(order)
def handle_auto_cancellation(order: Order) -> Refund:
return refund_to_original_payment(order)
if __name__ == "__main__":
order = Order("PB-1042", 1899, "card_7731", "cu_58")
print(handle_support_ticket(order))
print(handle_auto_cancellation(order))<?php
final class Order
{
public function __construct(
public readonly string $id,
public readonly int $totalCents,
public readonly string $paymentId,
public readonly string $customerId,
) {}
}
final class Refund
{
public function __construct(
public readonly string $orderId,
public readonly string $method,
public readonly int $amountCents,
public readonly string $destination,
) {}
}
function refundAsStoreCredit(Order $order): Refund
{
$bonus = intdiv($order->totalCents, 10);
return new Refund(
$order->id,
'store_credit',
$order->totalCents + $bonus,
'wallet:' . $order->customerId,
);
}
function refundToOriginalPayment(Order $order): Refund
{
return new Refund(
$order->id,
'original_payment',
$order->totalCents,
$order->paymentId,
);
}
function handleSupportTicket(Order $order): Refund
{
return refundAsStoreCredit($order);
}
function handleAutoCancellation(Order $order): Refund
{
return refundToOriginalPayment($order);
}
$order = new Order('PB-1042', 1899, 'card_7731', 'cu_58');
var_dump(handleSupportTicket($order));
var_dump(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
trueorfalse. - 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.
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.
Create
refundAsStoreCredit(order)whose body is justreturn issueRefund(order, true). Run the tests.Create
refundToOriginalPayment(order)whose body is justreturn issueRefund(order, false). Run the tests.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.
Search the codebase for any remaining calls to
issueRefund. If none remain, copy the store credit branch intorefundAsStoreCreditand the original payment branch intorefundToOriginalPayment, replacing the delegating calls. Run the tests.Delete
issueRefundand 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.