Programming Popular

Never Hardcode Magic Numbers

Code like "if (status === 3)" or "timeout = 86400" is meaningless to anyone reading it — including your future self. Replace magic numbers with named constants....

Problem

Code like "if (status === 3)" or "timeout = 86400" is meaningless to anyone reading it — including your future self.

Solution

Replace magic numbers with named constants. The name explains the purpose, and changing the value only requires one edit.

Benefit

Self-documenting code, fewer bugs when values change, and easier code reviews.

Code Example

// Bad — what does 3 mean? What is 86400?
if (order.status === 3) { ... }
setTimeout(cleanup, 86400000);

// Good — self-documenting
const STATUS_APPROVED = 3;
const ONE_DAY_MS = 24 * 60 * 60 * 1000;

if (order.status === STATUS_APPROVED) { ... }
setTimeout(cleanup, ONE_DAY_MS);

ES
Edrees Salih
6 hours ago

We are still cooking the magic in the way!