Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Make duration literals self-documenting with units
Bare numbers like 120_000 passed as TTLs/timeouts hide their unit and invite seconds-vs-milliseconds bugs. Name the constant with a unit suffix or compute it from labeled factors.
Bad example
| 1 | // Is 120_000 seconds or milliseconds? Does this API expect which? |
| 2 | await cache.set(key, value, 120_000); |
Explanation (EN)
A raw magic number gives no hint of its unit, so readers can't tell whether the TTL is 2 minutes or ~33 hours, and a unit mismatch with the API silently produces the wrong expiry.
Objašnjenje (HR)
Goli magicni broj ne daje naznaku jedinice, pa citatelji ne mogu znati je li TTL 2 minute ili ~33 sata, a neuskladjenost jedinice s API-jem tiho proizvodi pogresno isticanje.
Good example
| 1 | const TWO_MINUTES_MS = 2 * 60 * 1000; |
| 2 | // cache-manager v5+ expects the TTL in milliseconds |
| 3 | await cache.set(key, value, TWO_MINUTES_MS); |
Explanation (EN)
A named constant built from labeled factors states both the value and its unit, and a short comment pins the unit the API expects, so the intent is unambiguous and verifiable.
Objašnjenje (HR)
Imenovana konstanta sastavljena od oznacenih faktora navodi i vrijednost i jedinicu, a kratki komentar fiksira jedinicu koju API ocekuje, pa je namjera nedvosmislena i provjerljiva.
Notes (EN)
Always confirm the unit the target API expects (e.g. cache-manager v4 used seconds, v5+ uses milliseconds) — the constant name must match reality.
Bilješke (HR)
Uvijek potvrdite jedinicu koju ciljni API ocekuje (npr. cache-manager v4 je koristio sekunde, v5+ koristi milisekunde) — naziv konstante mora odgovarati stvarnosti.