Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Sort/normalize a collection before deriving a cache key from it
Order-insensitive inputs must be sorted before building a cache key, or equivalent requests miss the cache.
Bad example
| 1 | const cacheKey = `prices-${ids.join(',')}`; |
| 2 | // [1,2,3] and [3,2,1] are the same request but produce different keys |
Explanation (EN)
Different orderings of an equivalent collection can create avoidable cache misses.
Objašnjenje (HR)
Različiti redoslijedi ekvivalentne kolekcije mogu uzrokovati nepotrebne cache promašaje.
Good example
| 1 | const cacheKey = `prices-${[...ids].sort((a, b) => a - b).join(',')}`; |
| 2 | // [1,2,3] and [3,2,1] now map to the same key |
Explanation (EN)
Sorting validated numeric IDs on a copy canonicalizes ordering; duplicate handling still follows the query contract.
Objašnjenje (HR)
Sortiranje kopije validiranih brojčanih ID-jeva ujednačava redoslijed. Postupanje s duplikatima i dalje određuje ugovor upita.
Notes (EN)
Canonicalize a copy of a collection only when result semantics ignore order; define whether duplicates, types and missing values also affect identity.
Exceptions / Tradeoffs (EN)
Skip normalization only when order is semantically significant to the cached result. Sorting preserves duplicates and does not make an array a set. Deduplicate only when multiplicity is irrelevant; use an unambiguous encoding for arbitrary strings.
Iznimke / Tradeoffi (HR)
Normalizaciju preskoči kada redoslijed mijenja rezultat. Sortiranje ne uklanja duplikate i ne pretvara niz u skup; dedupliciraj samo ako broj ponavljanja nije važan. Za proizvoljne stringove koristi nedvosmisleno kodiranje.