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).
Guard possibly-nullish arrays before calling filter/map/reduce
Default nullable array fields to [] before chaining filter/map/reduce so absent external data does not throw at runtime.
Bad example
| 1 | // item.meta.tags comes from an external API payload and may be undefined |
| 2 | const columnTags = item.meta.tags |
| 3 | .filter((tag) => tag.tagTypeId === columnTypeId) |
| 4 | .map((tag) => tag.name) |
| 5 | .join(','); |
Explanation (EN)
If item.meta.tags is null or undefined, .filter() throws 'Cannot read properties of undefined', crashing the whole request. Type annotations don't protect you when the data originates from an untyped/external source or when strict null checks are disabled.
Objašnjenje (HR)
Ako je item.meta.tags null ili undefined, .filter() baca 'Cannot read properties of undefined' i ruši cijeli zahtjev. Tipovi te ne stite kada podaci dolaze iz netipiziranog/vanjskog izvora ili kada su strict null provjere iskljucene.
Good example
| 1 | // Default to an empty array so missing data degrades gracefully |
| 2 | const columnTags = (item.meta.tags ?? []) |
| 3 | .filter((tag) => tag.tagTypeId === columnTypeId) |
| 4 | .map((tag) => tag.name) |
| 5 | .join(','); |
Explanation (EN)
Defaulting to [] with the nullish coalescing operator keeps the chain valid even when the field is absent, yielding an empty result instead of a thrown exception. This is essential for data crossing a trust boundary (API responses, queue messages, DB rows).
Objašnjenje (HR)
Postavljanje [] kao zadane vrijednosti pomocu nullish coalescing operatora cuva lanac ispravnim i kada polje nedostaje, dajuci prazan rezultat umjesto izuzetka. Ovo je nuzno za podatke koji prelaze granicu povjerenja (API odgovori, poruke iz reda, retci baze).
Notes (EN)
Prefer ?? [] over || [] so that legitimately falsy-but-valid values are not unintentionally replaced; for arrays the distinction rarely matters but ?? is the safer default.
Bilješke (HR)
Preferiraj ?? [] umjesto || [] kako legitimno falsy ali valjane vrijednosti ne bi bile nenamjerno zamijenjene; za polja razlika rijetko dolazi do izrazaja, ali ?? je sigurniji izbor.
Exceptions / Tradeoffs (EN)
Skip the guard only when the type is genuinely non-nullable and locally constructed (e.g. a const array literal you just created), where a default would be dead code. Balance against remove-unnecessary-fallbacks-for-guaranteed-values: default to [] only for external/untrusted data; not for arrays the schema truly guarantees in-process.
Iznimke / Tradeoffi (HR)
Preskoci zastitu samo kada je tip stvarno ne-nullable i lokalno konstruiran (npr. const literal polja koji si upravo stvorio), gdje bi zadana vrijednost bila mrtvi kod.