Rules Hub
Code Ruleset
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)
Calling array methods on an absent optional array throws before rendering can recover.
Objašnjenje (HR)
Poziv metode niza nad odsutnim opcionalnim nizom baca pogrešku prije nego što se render može oporaviti.
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)
Default a known optional array to [] only when missing means an empty collection under the contract, after validating the parent structure.
Bilješke (HR)
Poznati opcionalni niz zamijeni s [] samo kada odsutnost prema ugovoru znači praznu kolekciju, nakon provjere roditeljske strukture.
Exceptions / Tradeoffs (EN)
Nullish fallback does not validate arbitrary JSON: strings/objects can still reach array methods. Preserve unavailable/error states when they differ from empty, and skip redundant fallbacks after trusted schema validation.
Iznimke / Tradeoffi (HR)
Nullish fallback ne validira proizvoljni JSON: string ili objekt i dalje mogu stići do metoda niza. Sačuvaj razliku između nedostupnosti, pogreške i praznog rezultata te izostavi suvišne fallbackove nakon pouzdane validacije sheme.