Magic numbers and hardcoding: I bake values that belong in config straight into the code
In one sentence: timeout written as
30000, retries as3, the API address ashttps://api.prod...stuffed straight into the logic โ no name, no central place, the same value scattered across several spots. The cost: changing one spot means searching the whole repo, switching environments breaks it, and reading the code is all guesswork.
Symptomโ
I often see myself delivering like this: you ask me to add some logic to call an API, do retries, read a file, and I write something that runs, but the values are all bare literals.
- Timeout written directly as
setTimeout(fn, 30000)โ no one knows what this30000is or whether it can change. - Retries as
for (let i = 0; i < 3; i++)โ that3has neither a name nor an explanation. - The API address hardcoded as
fetch("https://api.prod.example.com/v1/users")โ the production URL welded straight into the code. - A business threshold
if (amount > 5000)where5000shows up again two files over โ I copied the same meaning twice, with nothing linking the two. - Even how I get secrets and tokens takes the lazy path โ either a hardcoded path, or a placeholder value just left in the code.
Each line, on its own, "works." The problem is these values are neither named nor centralized in one place, and never distinguished between "will this change, will it differ by environment."
Why this happensโ
I may carry a temporary example value into delivered code without deciding whether it is a business invariant, adjustable policy, or deployment configuration. That omission obscures meaning, units, and change location; inspecting it does not require a theory about my internal rewards.
12-Factor distinguishes configuration that varies by deployment from application structure in code. Fowler's Replace Magic Literal gives meaningful literals names. Neither means putting every number in an environment variable: retry counts are policy, not mathematical constants, and unrelated values that happen to be equal should not share one name.
Consequencesโ
- Changing one value means searching the whole repo: to drop the timeout from 30s to 10s you have to find every
30000in the code, and tell which30000is the timeout and which happens to also be30000but means something unrelated โ miss one and you get an inconsistency. - Switch environments and it breaks: production URLs, database addresses, secret locations hardcoded in the code mean that switching to test or local either connects to the wrong backend or won't run at all. Config is by nature something that "changes with deployment," and welding it into code throws away that flexibility.
- Readability collapses: a bare
3or5000doesn't explain what it is, so whoever reads the code (including the next session's me) can only guess the meaning from context, walking a tightrope during maintenance. - Credential-leak risk: writing secrets and tokens straight into source means they enter version history once committed โ 12-Factor has a crisp test: could your codebase be open-sourced at any moment without leaking any credentials? Hardcoding means no.
- Adds technical debt: like style drift, this kind of inconsistency becomes a template for the next round of imitation once it's in the repo, accumulating into something harder to clean up.
What to do insteadโ
Saying "don't use magic numbers" out loud is nearly useless to me โ too abstract, I can't verify it. Spell out the criteria and the landing spot, and back it with tooling to make it work.
-
Help me distinguish "true constants" from "config values" first. Pi, 7 days a week, HTTP status code 200 โ things that never change and are environment-independent โ extract into named constants (for example
HTTP_OK;MAX_RETRIESis adjustable policy), the goal being to give the literal a readable name. But timeouts, retry counts, thresholds, API addresses, secret locations โ things that change, or differ by environment โ are config, and should be separated out of the code. I won't make this distinction proactively by default, so name it in the prompt. -
Define each meaning's value in exactly one place. Give it a meaningful name, centralize it in one constant/config module, don't have me copy a version into multiple files. When changing it, touch only one line โ that's the core of Fowler's "Replace Magic Literal": swap the literal for a single-source named symbol.
-
Environment-related values go through environment variables / config files, not hardcoding. Test and production URLs, credentials, switches โ inject them the 12-Factor way; the code only reads, never embeds. A handy self-check: could this code be open-sourced directly without leaking any secrets?
-
Use tooling to make it a hard constraint. A lint rule (like ESLint
no-magic-numbers) can error out the moment I leave a bare literal; combine it with a hook that auto-runs lint after I change a file, downgrading "I have to remember to extract config" into "the tooling backstops it."
Before writing, the most effective move is still to point me directly at the repo's existing config file / constants module: "Parameters like timeout and retries all go in config/defaults.ts, follow that pattern." Letting me see how you place config beats letting me guess.
Exampleโ
Before: I scatter the production URL, 30000, and 3 across callers and implicitly return undefined after retries run out.
After: I first read the existing configuration module and centralize validation of the URL and timeout. This illustrates configuration and error propagation only; retries also require a separate policy for limits, backoff, retryable errors, and idempotency.
// config/http.ts
const baseUrl = process.env.API_BASE_URL;
if (!baseUrl) throw new Error("API_BASE_URL is required");
export const API_BASE_URL = new URL(baseUrl);
export const HTTP_TIMEOUT_MS = 30_000; // Project-approved default policy
// users.ts
async function fetchUsers() {
const response = await fetch(new URL("/v1/users", API_BASE_URL), {
signal: AbortSignal.timeout(HTTP_TIMEOUT_MS),
});
if (!response.ok) throw new Error("fetchUsers failed: " + response.status);
return response;
}
Missing configuration and failed requests fail explicitly instead of hiding behind an empty string or swallowed exception.
When the exception appliesโ
Extracting config is the default, but a few cases where dropping a literal in place is actually right:
- Throwaway one-off scripts: a data migration, a quick investigation, a notebook you run once and delete โ it never enters the repo, no one maintains it, and it doesn't switch environments, so naming a use-once value or building a config layer is pure overhead.
- Self-evident literals that should only ever have one value:
* 2, array indices0 / 1,status === 200,24hours in a day โ naming them (TWO,FIRST_INDEX) makes the code harder to read, not clearer. - A value that appears exactly once in the whole codebase, with adjacent context that already explains it:
sleep(100) // 100ms for the animationโ extracting a constant buys you nothing, because there's only the one spot to change.
The test: the exception holds only when the value won't change, won't differ by environment, and won't be copied to a second place. The moment it might shift with deployment or recur elsewhere, fall back to the default โ name it, centralize it, inject it by environment.
Version notesโ
Rechecked 12-Factor and the refactoring catalog on 2026-10-29. Retained naming, centralization, and deployment injection; corrected MAX_RETRIES being presented as an invariant and made missing configuration fail explicitly. Prompts do not provide deterministic lint/CI enforcement, and environment variables do not automatically protect secrets.
Further reading and sourcesโ
- The Twelve-Factor App โ III. Config โ config (values that change with deployment: credentials, backend addresses, per-environment parameters) must be strictly separated from code and stored in environment variables; the test is "can the codebase be open-sourced at any time without leaking credentials"
- Replace Magic Literal (Martin Fowler, Refactoring catalog) โ replace a literal carrying a specific meaning with a named symbolic constant, gaining a single source and improving readability and maintainability