A logbook for curious waters
← Software Engineering Field Guide

Inline Temp

Before you begin: Variables, assignment, and return values.

Inline Temp, also called Inline Variable, replaces a temporary variable with the expression assigned to it. It is useful when the variable adds no explanation and is not needed to preserve one particular computed value. 1

Original version: store an answer only to return it

A name-cleaning function removes leading and trailing whitespace. Assume a non-null string.

public static class Names
{
    public static string Clean(string name)
    {
        string result = name.Trim();
        return result;
    }
}

result is assigned once and immediately returned. Its name tells us nothing beyond what return already says.

def clean_name(name):
    result = name.strip()
    return result

result is assigned once and immediately returned. It has no other use, and no intervening operation can change what it represents.

Refactored version: return the expression

public static class Names
{
    public static string Clean(string name)
    {
        return name.Trim();
    }
}

The trim operation still runs once. Names.Clean(" Mira ") still returns Mira.

def clean_name(name):
    return name.strip()

The strip operation still runs once. clean_name(" Mira ") still returns Mira.

A temporary can preserve meaning or timing

Do not replace every local variable simply because it can be replaced. distanceMeters may explain a formula; now may deliberately capture one clock reading for several comparisons.

If a temporary stores the result of a database request and appears three times, replacing each use with the request would perform three requests. Even a property getter can have cost or side effects. A temporary assigned more than once also requires understanding which value reaches each use.

In this example there is one assignment, one use, and no meaningful name to preserve. That is why inlining is safe and modestly clearer. The reverse refactoring, Extract Variable, is appropriate when naming an expression would teach the reader something.

Sources

  1. Martin Fowler — Inline Variable, the modern name for Inline Temp.