Skip to content
Clean CodeChapter 13 / 57Beginner5 min read

Constants & Magic Values

Replace magic numbers with named constants. Keep status strings, limits and keys in const objects and union types, with TypeScript examples.

On this page

1. What is this practice?

A magic value is a literal that appears in code out of nowhere and does not explain itself: if (attempts > 5), status === "pnd", setTimeout(fn, 86400000). The reader wonders: why 5? What is "pnd"? How long is that number?

KindExampleBetter
Limit5MAX_LOGIN_ATTEMPTS
Duration86400000ONE_DAY_MS
Status string"pending" in every fileORDER_STATUS.pending or a union type
Storage keylocalStorage.getItem("tkn")STORAGE_KEYS.token
Route"/dashboard/settings"ROUTES.settings

2. Why does it matter?

Magic values have two costs: the meaning is hidden, and they are hard to change. If the limit must go from 5 to 10, you search for every 5 and wonder which one belongs to login and which to something else.

Magic valuesNamed constants
A typo like pendng instead of pending gives no error, the bug hidesTypeScript reports the typo right away
What 5 means is a guessMAX_LOGIN_ATTEMPTS explains itself
Changing a limit means searching the whole repoChange one constant
Different values in two files, the UI and the API disagreeOne source, one answer
A new status is added, some places do not handle itA Record type shows where it is missing

3. When to follow it

LevelHow importantWhy
Learning projectImportantAn easy habit that saves you from typo bugs
Production codeStatuses, limits and keys are always namedRepeated literals are the most common drift bug
Senior levelDesign where constants liveA module's constants next to its types, shared ones in one place

4. Bad vs good code

ts
async function login(email: string, password: string) {
  const user = await users.byEmail(email);

  if (user.failedAttempts >= 5) {
    throw new Error("locked");
  }

  if (user.status === "suspnded") {
    throw new Error("suspended");
  }

  localStorage.setItem("tkn", await auth.token(user, password));
  setTimeout(logout, 1800000);
}
ts
export const MAX_LOGIN_ATTEMPTS = 5;
export const SESSION_TIMEOUT_MS = 30 * 60 * 1000;

export const USER_STATUS = {
  active: "active",
  suspended: "suspended",
} as const;

export type UserStatus = (typeof USER_STATUS)[keyof typeof USER_STATUS];

export const STORAGE_KEYS = {
  token: "auth_token",
} as const;
ts
async function login(email: string, password: string) {
  const user = await users.byEmail(email);

  if (user.failedAttempts >= MAX_LOGIN_ATTEMPTS) {
    throw new LockedError(user.id);
  }

  if (user.status === USER_STATUS.suspended) {
    throw new SuspendedError(user.id);
  }

  localStorage.setItem(STORAGE_KEYS.token, await auth.token(user, password));
  setTimeout(logout, SESSION_TIMEOUT_MS);
}
What is the difference?BadGood
5What is it?MAX_LOGIN_ATTEMPTS
"suspnded"A typo that never matches, so a suspended user can log inUSER_STATUS.suspended, a typo is a compile error
1800000Count the zeros30 * 60 * 1000, reads as 30 minutes
"tkn"Can be spelled differently in every fileOne key, one place

The typo in the first version is a security bug: a suspended user can log in, and no error appears.

A typed accessor, not a magic key

ts
const theme = store.get("thme");
ts
const theme = settings.theme();

When the key sits behind a typed function, writing the wrong key is simply impossible.

5. How to apply it

When you see a literal
You spot a number or string in the code
Is its meaning clear without thinking?
No: give it a name
Is it part of a set, like statuses? Use an as const object or a union type
Does it appear in several files? Put it in the module's types or constants file
Write durations as a calculation, like 30 * 60 * 1000

6. Exceptions and common mistakes

Not every literal is magic. 0, 1, an empty string, index + 1, or array.length === 0 all have an obvious meaning. Example values in test files ("test@example.com") are fine too.

It is also good to keep text shown to users in one place (needed for translation), but making a constant for every button label is not necessary in a small project.

Mistake: Naming a constant after its value

Why? FIVE = 5 or SECONDS_60 explains nothing, and if the value changes tomorrow the name will lie.

Better: Name it after the purpose, like MAX_LOGIN_ATTEMPTS, not the value.

Mistake: One constants.ts holding the constants of the whole app

Why? Every module's values end up in one file, the file gets huge and everyone edits it.

Better: Keep each constant next to the module that uses it, and only truly shared ones in a shared file.

Mistake: Mixing TypeScript enums and string unions

Why? With an enum in one place and a string in another, comparisons and imports get confusing.

Better: Pick one pattern for the repo, like an as const object plus a derived union type.

Mistake: Keeping a status as an untyped string

Why? status: string accepts any typo.

Better: Give status a union type so only valid values are allowed.

7. What to learn before and after

Final quick revision

CheckQuestion
MeaningIs the meaning of every number and string clear?
NameDoes the constant name describe the purpose, not the value?
SetAre statuses an as const object or a union type?
One placeIs a repeated value defined in only one place?
KeysAre storage and state keys behind a typed accessor?