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).
Enforce time-window filtering explicitly, not via storage TTL
Filter output by the relevant timestamp instead of trusting a TTL that other writes can refresh.
Bad example
| 1 | // Relies on a 48h TTL to keep only recent entries |
| 2 | const entries = Object.values(state) |
| 3 | .sort((a, b) => b.publishedAt.localeCompare(a.publishedAt)) |
| 4 | .slice(0, MAX_ENTRIES); |
| 5 | // upsert refreshes TTL on every write, so old items can survive |
Explanation (EN)
A later update refreshes the TTL, so an item published outside the window stays alive and leaks into the output when there are fewer than MAX_ENTRIES recent items.
Objašnjenje (HR)
Kasniji update osvježi TTL, pa stavka objavljena izvan prozora ostaje živa i procuri u izlaz kada postoji manje od MAX_ENTRIES nedavnih stavki.
Good example
| 1 | const cutoff = Date.now() - WINDOW_MS; |
| 2 | const entries = Object.values(state) |
| 3 | .filter((e) => new Date(e.publishedAt).getTime() >= cutoff) |
| 4 | .sort((a, b) => b.publishedAt.localeCompare(a.publishedAt)) |
| 5 | .slice(0, MAX_ENTRIES); |
Explanation (EN)
An explicit cutoff filter at read time guarantees only in-window records appear, independent of how storage TTLs behave.
Objašnjenje (HR)
Eksplicitni filtar po granici pri čitanju jamči da se pojavljuju samo zapisi unutar prozora, neovisno o tome kako se ponašaju TTL-ovi pohrane.