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).
Prefer non-mutating slice over in-place splice for truncation
Use slice to take the first N elements instead of splice, which mutates the original array and can cause hidden side effects.
Bad example
| 1 | const top = events.sort(byDateDesc); |
| 2 | top.splice(50); // mutates `events` and returns the removed tail, not the kept items |
Explanation (EN)
splice mutates the source array in place and returns the discarded elements, which is easy to misread and dangerous if `events` is shared.
Objašnjenje (HR)
splice mijenja izvorni niz na licu mjesta i vraća odbačene elemente, što se lako krivo pročita i opasno je ako se `events` dijeli.
Good example
| 1 | const top = events.slice().sort(byDateDesc).slice(0, 50); |
Explanation (EN)
slice returns a new array of the kept items without touching the original, making the intent and the side-effect-free behaviour explicit.
Objašnjenje (HR)
slice vraća novi niz zadržanih elemenata bez diranja originala, čime su namjera i odsutnost nuspojava eksplicitne.
Notes (EN)
In-place truncation is acceptable only when the array is provably local and never read again afterwards.
Bilješke (HR)
Skraćivanje na licu mjesta prihvatljivo je samo kad je niz dokazivo lokalan i nakon toga se nikad više ne čita.
Exceptions / Tradeoffs (EN)
Balance against push-into-grouped-map-array-not-spread: prefer non-mutating slice for truncating a shared/returned array where immutability matters.