Programming Search⌘K

Techniquecore

Move statements into function

When the same statements run each time a function is called, move them into the function, so that callers cannot forget them; the inverse of Move Statements to Callers.

Family
refactoring
Intent
Make code that always goes with a call part of the function it goes with.

Purpose

If every caller of a function writes the same lines before or after the call, those lines are part of what the function means. Left in the callers, they are duplicated code, and a caller that forgets them is a bug.

Move statements into function puts them inside. The catalogue card moves the line that writes a photo's title into the photoData function that already writes its location and date.1 The inverse is Move Statements to Callers.1

The book's test is whether the statements make sense only as part of the called function. If they do, move them in. If they do not, but they still always run with the call, the better fix is Extract Function on the statements and the call together.2

Steps

  1. If the repeated statements are not next to the call, move them there with Slide Statements.2
  2. If the function has only one caller, cut the statements from the caller, paste them into the function, and test. Stop here.
  3. Otherwise, at one call site, use Extract Function on the call and the statements together. Give the result a temporary name.2
  4. Change each other caller to use the extracted function. Test after each.
  5. When all callers use it, inline the original function into the extracted one with Inline Function.2
  6. Rename the extracted function to the original name with Change Function Declaration, and test.

Example

Two callers print an address block, and each first writes the recipient's name above the lines that addressLines returns. The name moves into the function, which now takes the recipient.

import assert from "node:assert/strict";

const ana = { name: "Ana Pires", address: { street: "Rua Augusta 10", city: "Lisboa", postcode: "1100-053" } };

// Before: each caller writes the name, then calls the function.
function addressLinesOld(a) { return [a.street, `${a.postcode} ${a.city}`]; }
const labelOld = [ana.name, ...addressLinesOld(ana.address)];
const invoiceOld = ["Bill to:", ana.name, ...addressLinesOld(ana.address)];

// After: the name is part of the address block.
function addressBlock(r) { return [r.name, r.address.street, `${r.address.postcode} ${r.address.city}`]; }
const label = addressBlock(ana);
const invoice = ["Bill to:", ...addressBlock(ana)];

assert.deepEqual(label, labelOld);
assert.deepEqual(invoice, invoiceOld);
console.log("move statements into function: tests pass");
Output
move statements into function: tests pass

The invoice still adds its own "Bill to:" line. That line varies between callers, so it stays outside.

Language What the example shows
JavaScript a repeated line moved into the function, with the varying line left outside, as above
Python the mechanics for several callers: extract at one call site, switch the others, inline the old function, rename
Go the same move, where the function's parameter changes from the address to the recipient
PythonGitHub
ana = {"name": "Ana Pires", "address": {"street": "Rua Augusta 10", "city": "Lisboa", "postcode": "1100-053"}}


# Before: two callers write the name before each call.
def address_lines(a):
    return [a["street"], f"{a['postcode']} {a['city']}"]


label_before = [ana["name"], *address_lines(ana["address"])]
invoice_before = ["Bill to:", ana["name"], *address_lines(ana["address"])]


# Step 3: extract the call and the name together, with a temporary name.
def address_block_tmp(r):
    return [r["name"], *address_lines(r["address"])]


# Step 5: inline address_lines into it. Step 6: rename to address_block.
def address_block(r):
    a = r["address"]
    return [r["name"], a["street"], f"{a['postcode']} {a['city']}"]


assert address_block_tmp(ana) == label_before
assert address_block(ana) == label_before
assert ["Bill to:", *address_block(ana)] == invoice_before
print("move statements into function: tests pass")
Output
move statements into function: tests pass
package main

import (
	"fmt"
	"slices"
)

type Address struct{ Street, City, Postcode string }

type Recipient struct {
	Name    string
	Address Address
}

// After: the name line moved in, so the parameter is now the recipient.
func AddressBlock(r Recipient) []string {
	return []string{r.Name, r.Address.Street, r.Address.Postcode + " " + r.Address.City}
}

func main() {
	ana := Recipient{"Ana Pires", Address{"Rua Augusta 10", "Lisboa", "1100-053"}}
	want := []string{"Ana Pires", "Rua Augusta 10", "1100-053 Lisboa"}
	if got := AddressBlock(ana); !slices.Equal(got, want) {
		panic(fmt.Sprint(got))
	}
	invoice := append([]string{"Bill to:"}, AddressBlock(ana)...)
	if len(invoice) != 4 {
		panic(invoice)
	}
	fmt.Println("move statements into function: tests pass")
}
Output
move statements into function: tests pass

When to use

  • The same lines before or after each call. They are part of the function.2
  • A caller that forgot. A bug from one caller that did not write the lines is a sign that they belong inside.
  • After moving functions. When a function moves, the statements around its calls can be part of the move.

Pitfalls

Statements that vary. If even one caller needs different statements, moving them in forces a flag parameter or a copy of the function. Leave those statements outside, or keep the variation in the caller.

Order and side effects. Sliding the statements next to the call can change the order of side effects. Check that nothing between them depends on the order before step 1.

A changed signature. If the moved lines use data that the function did not have, the function needs a parameter for it, as in the example. Callers then change too.

Tools that do it

This refactoring is a sequence of steps, not one action of a tool. The steps use Extract Function, Inline Function and Change Function Declaration; see those pages for the tools.

References

  1. Martin Fowler, Catalog of Refactorings, Move Statements into Function, the entry; inverse of Move Statements to Callers
  2. Martin Fowler, Refactoring, Improving the Design of Existing Code, second edition, Addison-Wesley, 2018, Chapter 8, Moving Features, Move Statements into Function, motivation and mechanics

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)
WritingW3 standard
  • at least three sources