Taming Magic Numbers With A Constants Toolbox!
On our project everything revolves around constants: story auto-close timers, retry limits, AB split ratios, compression thresholds. When those values hide across the codebase, we lose hours chasing “mysterious 42s”. A dedicated constants module gave us clarity and spared the team from accidental regressions.
Future maintainers cannot guess what 3000 means. Timeout? Character limit? Analytics sampling? A named constant like STORY_AUTO_CLOSE_TIMEOUT_MS answers instantly.
Updating a value means hunting every single occurrence. Miss one — and the feature breaks. A single entry inside constants.ts lets us change it once without collateral damage.
Frontend, backend, analytics, and marketing teams need the same numbers. Shared constants remove disagreements and simplify documentation.
// constants/timeouts.ts
export const STORY_AUTO_CLOSE_TIMEOUT_MS = 3000;
export const NOTIFICATION_HIDE_DELAY_MS = 5000;
export const ANALYTICS_BATCH_SIZE = 50;timeouts, featureFlags, experiments, pricing — logical folders make navigation obvious.shared-config, or environment service.Every one of these deserves a named constant and a comment.
Environments add another trap. Dev, stage, trunk, and production run on different base URLs, API keys, and sometimes separate event buses. Constants like DEV_API_BASE_URL, STAGE_CUSTOMER_PORTAL_URL, and PROD_SUBSCRIPTION_PACKAGE_IDS prevent misconfigured builds. When the base API changes, you update a single entry and keep the whole crew aligned.
constants module and an eslint rule like no-magic-numbers to catch offenders.Magic numbers are silent debt. Create a central constants file, annotate every value, and link the source of truth. The next engineer will know why the timer sits at 3 seconds, product will trust the coefficients, and releases will stay predictable. Less magic, more clarity, happier team.