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).
Reset arrays with reassignment, not length mutation
Prefer arr = [] over arr.length = 0 to clear an array unless a shared reference must stay live.
Bad example
| 1 | function reset() { |
| 2 | cache.length = 0; |
| 3 | } |
Explanation (EN)
Mutating .length to clear an array is a less obvious idiom and reads as a side effect on a property rather than a reset.
Objašnjenje (HR)
Mutiranje .length za praznjenje polja manje je ociti idiom i cita se kao nuspojava na svojstvu, a ne kao resetiranje.
Good example
| 1 | function reset() { |
| 2 | cache = []; |
| 3 | } |
Explanation (EN)
Reassigning a fresh array states intent clearly and avoids questions about lingering references or memory.
Objašnjenje (HR)
Dodjeljivanje novog polja jasno izrazava namjeru i izbjegava pitanja o zaostalim referencama ili memoriji.
Exceptions / Tradeoffs (EN)
Use arr.length = 0 when another part of the code holds a reference to the same array instance and must observe the cleared state in place.
Iznimke / Tradeoffi (HR)
Koristi arr.length = 0 kada drugi dio koda drzi referencu na istu instancu polja i mora vidjeti ocisceno stanje na licu mjesta.