Programming Search⌘K

Techniquecore

Remove flag argument

Also called replace parameter with explicit methods.

Replace an argument that a caller passes only to select what a function does with one explicit function for each choice.

Family
refactoring
Intent
Say at the call which operation you want.
Also called
Replace Parameter with Explicit Methods

Purpose

A call such as book(customer, true) does not say what true means. The reader must open the function to find out. Fowler defines the problem: "A flag argument is a kind of function argument that tells the function to carry out a different operation depending on its value."2

Remove flag argument gives each value of the flag its own function. The catalogue card turns setDimension(name, value), which tests whether name is the height or the width, into setHeight(value) and setWidth(value).1 Fowler's reason is that "the separate methods communicate more clearly what my intention is when I make the call."2

Steps

  1. Make one explicit function for each value of the flag. If the body is a conditional on the flag, use Decompose Conditional to move each branch into its own function. See Decompose Conditional.
  2. Change each caller that passes a literal for the flag to call the matching explicit function. Test after each change.
  3. When no caller passes the flag, remove the old function.

If some callers pass the flag from a variable, for example from a form field, keep the old function for them, or let it call the explicit functions.

Example

A booking function takes a boolean that selects a premium booking. After the refactoring, callers call regularBooking or premiumBooking. The shared work stays in one place.

import assert from "node:assert/strict";

function bookBefore(customer, isPremium) {
  const booking = { customer, seat: "standard", lounge: false };
  if (isPremium) { booking.seat = "front"; booking.lounge = true; }
  return booking;
}

function baseBooking(customer) {
  return { customer, seat: "standard", lounge: false };
}

function regularBooking(customer) {
  return baseBooking(customer);
}

function premiumBooking(customer) {
  return { ...baseBooking(customer), seat: "front", lounge: true };
}

assert.deepEqual(regularBooking("Ana"), bookBefore("Ana", false));
assert.deepEqual(premiumBooking("Ana"), bookBefore("Ana", true));
console.log(premiumBooking("Ana"));
Output
{ customer: 'Ana', seat: 'front', lounge: true }

The call premiumBooking("Ana") says what bookBefore("Ana", true) hid.

Language What the example shows
JavaScript a boolean flag replaced with regularBooking and premiumBooking, as above
Python the same split; a keyword-only flag (*, premium=False) is shown as the smaller fix when the flag must stay
Go the same split; Go has no keyword arguments, so a flag call reads book("Ana", true)
PythonGitHub
def book_before(customer: str, is_premium: bool) -> dict:
    booking = {"customer": customer, "seat": "standard", "lounge": False}
    if is_premium:
        booking.update(seat="front", lounge=True)
    return booking


def base_booking(customer: str) -> dict:
    return {"customer": customer, "seat": "standard", "lounge": False}


def regular_booking(customer: str) -> dict:
    return base_booking(customer)


def premium_booking(customer: str) -> dict:
    return {**base_booking(customer), "seat": "front", "lounge": True}


# When the flag must stay, make it keyword-only, so that each call names it.
def book(customer: str, *, premium: bool = False) -> dict:
    return premium_booking(customer) if premium else regular_booking(customer)


assert regular_booking("Ana") == book_before("Ana", False)
assert premium_booking("Ana") == book_before("Ana", True)
assert book("Ana", premium=True) == premium_booking("Ana")
try:
    book("Ana", True)
except TypeError as e:
    print(type(e).__name__)
print(premium_booking("Ana"))
Output
TypeError
{'customer': 'Ana', 'seat': 'front', 'lounge': True}
package main

import "fmt"

type Booking struct {
	Customer string
	Seat     string
	Lounge   bool
}

// bookBefore takes a flag. At the call, book("Ana", true) does not say what true means.
func bookBefore(customer string, isPremium bool) Booking {
	b := Booking{Customer: customer, Seat: "standard"}
	if isPremium {
		b.Seat, b.Lounge = "front", true
	}
	return b
}

func regularBooking(customer string) Booking {
	return Booking{Customer: customer, Seat: "standard"}
}

func premiumBooking(customer string) Booking {
	b := regularBooking(customer)
	b.Seat, b.Lounge = "front", true
	return b
}

func main() {
	if regularBooking("Ana") != bookBefore("Ana", false) || premiumBooking("Ana") != bookBefore("Ana", true) {
		panic("mismatch")
	}
	fmt.Printf("%+v\n", premiumBooking("Ana"))
}
Output
{Customer:Ana Seat:front Lounge:true}

When to use

  • A boolean or enum literal at the call site that selects an operation.2
  • A flag that most callers pass as a constant. The constant is the name of the function that the caller wants.
  • A setter that takes a boolean, such as setSwitch(on). Fowler prefers setOn() and setOff() in most cases.2

Pitfalls

Tangled logic. When the flag is tested in many places in the body, the explicit functions can duplicate most of it. Fowler's answer is to keep the flagged function private: "one option is to retain the method with the flag argument, but keep it hidden."2 The explicit functions call it, and no other code does.

The value comes from data. If the caller reads the flag from a UI control or a file, it then needs an if to pick the function. For that case Fowler writes that he would "rather have setSwitch(aValue)".2

The flag is derived. If the choice depends only on the state of an argument, such as the status of the customer, the caller does not need to choose. Then the function can decide by itself, and no flag and no second function are needed.2

A value is not a flag. A parameter that the function uses in a calculation, such as a rate, is not a flag. To merge functions that differ in such a value, use Parameterize Function.

Tools that do it

No IDE has one action for this refactoring. In a typed language, the IDE can find each call that passes a literal for the parameter, and Change Function Declaration can remove the parameter at the end. See Change Function Declaration.

The catalogue lists the refactoring in the first edition, the second edition and the Ruby edition, with the tags refactoring-apis and remove.3 In the second edition it is in Chapter 11, Refactoring APIs.4 The smell that it fixes is Boolean parameter.

References

  1. Martin Fowler, Catalog of Refactorings, Remove Flag Argument, the entry, the setDimension sketch; aliases Replace Parameter with Explicit Methods
  2. Martin Fowler, FlagArgument, 23 June 2011, the whole note, with the sections Tangled Implementation, Deriving the flag and Boolean Setting Method
  3. Martin Fowler, Catalog of Refactorings, index, the entry removeFlagArgument, its books and tags
  4. Martin Fowler, Refactoring, Improving the Design of Existing Code, second edition, Addison-Wesley, 2018, Chapter 11, Refactoring APIs, Remove Flag Argument

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/H3HFC3/text.yaml