Replace Magic Literal

Symptom

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.

Goal

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

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

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.

Tradeoff

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

Relief

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.

Trap

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.