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).
A cache/comparison key must include the same fields on every call site, including defaulted ones
When building a request/cache key from an object of optional-but-defaulted fields, make sure every call site that builds that key for the 'same' request includes the same set of fields — a field that defaults to a truthy value rather than being fully omitted still has to appear identically on both sides, or the keys can never compare equal.
Bad example
| 1 | // preload — omits `order` |
| 2 | const preloadKey = buildRequestKey({ id, page, pageSize }); |
| 3 |
|
| 4 | // active-tab fetch — always includes `order` (defaults to DESC) |
| 5 | const fetchKey = buildRequestKey({ id, page, pageSize, order: sortOrder }); |
| 6 |
|
| 7 | if (preloadKey === fetchKey) { /* never true, since order defaults truthy */ } |
Explanation (EN)
Because `order` always ends up truthy on one side and absent on the other, the keys can never match, so the cache never hits and a redundant fetch fires every time.
Objašnjenje (HR)
Budući da `order` uvijek ispadne istinit na jednoj strani, a odsutan na drugoj, ključevi se nikad ne mogu poklopiti, pa keš nikad ne pogađa i suvišan dohvat se pokreće svaki put.
Good example
| 1 | // both call sites include the same fields, including order |
| 2 | const preloadKey = buildRequestKey({ id, page, pageSize, order: sortOrder }); |
| 3 | const fetchKey = buildRequestKey({ id, page, pageSize, order: sortOrder }); |
| 4 |
|
| 5 | if (preloadKey === fetchKey) { /* correctly hits when nothing relevant changed */ } |
Explanation (EN)
Both keys are built from the exact same field set, so they compare equal whenever the underlying request would genuinely be the same, letting the cache hit as intended.
Objašnjenje (HR)
Oba ključa grade se od potpuno istog skupa polja, pa se poklapaju kad god bi zahtjev zaista bio isti, omogućujući kešu da pogodi kako je namijenjeno.