Programming Search⌘K

Techniquecore

Inline variable

Also called Inline Temp.

Replace the uses of a local variable with the expression that it holds, then delete the variable, when the name says no more than the expression.

Family
refactoring
Intent
Remove a local variable whose name adds nothing to its expression, or that stands in the way of another refactoring.
Also called
Inline Temp

Purpose

A local variable is a name for a value. Often the name helps: basePrice says more than order.quantity * order.itemPrice. Sometimes it does not: const price = order.price; return price > 1000; gives the reader a second name for the same thing.

Inline variable replaces each use of the variable with its expression and deletes the declaration. The second edition of Refactoring gives two reasons: the name communicates no more than the expression, and the variable gets in the way of refactoring the code around it.1 A common case of the second is Extract Function: a fragment that uses a local variable needs it as a parameter, and inlining the variable first can make the extraction cleaner.

The refactoring changes the structure and keeps the behaviour. Its inverse is Extract Variable, and the first edition called it Inline Temp.23

Steps

  1. Run the tests and see them pass.
  2. Check that the expression on the right has no side effect, and that its value is the same at each use as at the declaration. If something between the declaration and a use changes a value that the expression reads, the inline changes the result.
  3. If the variable is not already immutable, make it so (const or final) and compile or test. This finds any later assignment.
  4. Replace the first use with the expression, and test.
  5. Replace each other use in the same way, testing after each.
  6. Delete the declaration and the assignment, and test.

Example

A function names a value that it reads once, and another function caches a value that it then changes.

import assert from "node:assert/strict";

// Before: a variable that adds no meaning.
function isLargeBefore(order) {
  const basePrice = order.basePrice;
  return basePrice > 1000;
}

// After: the expression in place.
function isLarge(order) {
  return order.basePrice > 1000;
}

for (const basePrice of [0, 1000, 1001]) {
  assert.equal(isLarge({ basePrice }), isLargeBefore({ basePrice }));
}

// Not every variable can be inlined. Here the expression reads a value
// that changes between the declaration and the use.
function totalWithFee(cart) {
  const subtotal = cart.items.reduce((s, i) => s + i, 0);
  cart.items.push(5); // the fee is an item too
  return subtotal + 5;
}
function totalWithFeeWrong(cart) {
  cart.items.push(5);
  return cart.items.reduce((s, i) => s + i, 0) + 5; // inlined after the push
}
assert.equal(totalWithFee({ items: [10, 20] }), 35);
assert.equal(totalWithFeeWrong({ items: [10, 20] }), 40);
console.log("inline variable: tests pass");
Output
inline variable: tests pass

The first inline is safe: the expression is a property read, and nothing changes it between the declaration and the use. The second shows the check of step 2. subtotal holds the sum before the fee is pushed, and the inlined expression would sum after it, so the variable must stay.

Language What the example shows
JavaScript a variable inlined, and a variable that cannot be, as above
Python the same two cases
Go the same two cases, and the shadowing case that gopls refuses
PythonGitHub
# Before: a variable that adds no meaning.
def is_large_before(order):
    base_price = order["base_price"]
    return base_price > 1000

# After: the expression in place.
def is_large(order):
    return order["base_price"] > 1000

for p in (0, 1000, 1001):
    assert is_large({"base_price": p}) == is_large_before({"base_price": p})

# A variable that must stay: the list changes between the declaration and the use.
def total_with_fee(cart):
    subtotal = sum(cart)
    cart.append(5)  # the fee is an item too
    return subtotal + 5

def total_with_fee_wrong(cart):
    cart.append(5)
    return sum(cart) + 5  # inlined after the append

assert total_with_fee([10, 20]) == 35
assert total_with_fee_wrong([10, 20]) == 40
print("inline variable: tests pass")
Output
inline variable: tests pass
package main

import "fmt"

type order struct{ basePrice int }

// Before: a variable that adds no meaning.
func isLargeBefore(o order) bool {
	basePrice := o.basePrice
	return basePrice > 1000
}

// After: the expression in place.
func isLarge(o order) bool {
	return o.basePrice > 1000
}

func sum(xs []int) int {
	t := 0
	for _, x := range xs {
		t += x
	}
	return t
}

// A variable that must stay: the slice changes between the declaration and the use.
func totalWithFee(items []int) int {
	subtotal := sum(items)
	items = append(items, 5) // the fee is an item too
	return subtotal + 5
}

func totalWithFeeWrong(items []int) int {
	items = append(items, 5)
	return sum(items) + 5 // inlined after the append
}

// Shadowing: inside the block, x names another variable, so s cannot
// be replaced by its initializer there. gopls refuses this inline.
func shadow(x int) (string, string) {
	s := fmt.Sprintf("+%d", x)
	{
		x := 123
		return s, fmt.Sprintf("+%d", x)
	}
}

func main() {
	for _, p := range []int{0, 1000, 1001} {
		if isLarge(order{p}) != isLargeBefore(order{p}) {
			panic("behaviour changed")
		}
	}
	if totalWithFee([]int{10, 20}) != 35 || totalWithFeeWrong([]int{10, 20}) != 40 {
		panic("unexpected totals")
	}
	kept, inlined := shadow(7)
	fmt.Println(kept, inlined)
	fmt.Println("inline variable: tests pass")
}
Output
+7 +123
inline variable: tests pass

When to use

  • **The name says no more than the expression.**1 This is often a variable that holds the result of a call and is used once on the next line.
  • The variable is in the way. It blocks an Extract Function, or a Replace Temp with Query, or it ties two parts of a function that should be split.1
  • After Extract Variable went too far. A chain of variables each used once can read worse than the expression it replaced.

Keep the variable when the name explains, when the expression is expensive and used more than once, or when its value must be taken at a particular moment.

Pitfalls

A value that changes. The variable holds the value at the moment of the declaration. If the code between the declaration and a use changes what the expression reads, the inlined expression computes something else. The example shows this.

Side effects and cost. An expression that calls a function with an effect, or that is slow, runs once in the variable and once per use after the inline.

Shadowing. A name in the expression can mean something else at the point of use, if an inner block declares a variable with the same name. The gopls documentation shows this case and says that the code action "reports an error if it is not possible to make the transformation because one of the identifiers within the initializer expression" is "shadowed by an intervening declaration".4

Tools that do it

Tool Languages What it automates
IntelliJ IDEA Java, Kotlin Inline Variable, which "replaces redundant variable usage with its initializer"; "The variable must be initialized at declaration."5
gopls Go refactor.inline.variable, which replaces "the reference by the variable’s initializer expression"4
rust-analyzer Rust inline_local_variable, which "Inlines a local variable."6

The tools differ in how much they check. IntelliJ IDEA notes that if the initial value is modified, only the uses before the change are inlined.5 gopls "always replaces the reference by the initializer expression, even if there are later assignments to the variable", and it leaves the declaration for the programmer to delete.4

References

  1. Martin Fowler, Refactoring, Improving the Design of Existing Code, second edition, Addison-Wesley, 2018, Chapter 6, A First Set of Refactorings, Inline Variable, motivation and mechanics
  2. Martin Fowler, Catalog of Refactorings, Inline Variable, the entry; inverse of Extract Variable; alias Inline Temp
  3. Martin Fowler, Refactoring, Improving the Design of Existing Code, Addison-Wesley, 1999, Chapter 6, Composing Methods, Inline Temp
  4. gopls documentation, Code transformation features, Inline local variable, refactor.inline.variable
  5. IntelliJ IDEA documentation, Inline, Inline Variable
  6. rust-analyzer manual, Assists, inline_local_variable

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/RDM2X3/text.yaml