Replace Magic Literal

Compare
Symptom
Human

Numbers (1.609, 86400, 0.05) and strings ('admin', 'USD') inline in code where the value carries domain meaning that the literal alone can't express.

Agent

The agent encounters a bare number or string whose meaning requires loading the surrounding context to interpret; refactoring the value means finding every occurrence by character match.

Goal
Human

Bare numbers and strings that encode domain concepts become named constants whose name says what the value represents.

Agent

Each domain value has a named constant at one declaration site; every usage resolves through the constant's name, and the value's meaning loads with the name instead of being inferred from context at every literal occurrence.

Before the refactoring

function trip(distance) {
return distance * 1.609;
}

After the refactoring

const KM_PER_MILE = 1.609;
function trip(distance) {
return distance * KM_PER_MILE;
}
Example source: Illustrative example written for this site, not a quotation from any source.
Pressure
Human

Searching by domain term ('kilometers per mile') doesn't find the conversion; changing the value means hunting every occurrence; comments accumulate to explain what the constant means.

Agent

The agent must trace context to interpret bare literals; changing a value requires text-search across the codebase with no semantic guarantee of completeness.

Tradeoff
Human

Naming every literal can drown the file in trivia — only name literals that carry domain meaning the surrounding code can't speak.

Agent

Each named constant adds an import the agent loads at every consumer; the cost is one file load per consumer file in exchange for one definition site for the value.

Relief
Human

Searches by domain term find every callsite; changing the value is one edit; the constant invites code-side documentation when it's truly load-bearing.

Agent

Changing the value happens at one constant declaration; the agent does not grep for every literal occurrence and verify each by hand, and generated code that references the constant by name picks up the new value at the next build.

Trap
Human

Naming every literal — including loop indices, array offsets, status code 200 — drowns the file in trivia and breaks the eye's pattern-recognition for genuinely meaningful constants.

Agent

Naming every literal — including indices, loop bounds, and obvious status codes — bloats the agent's mental constant table without comprehension gain.