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).
Coalesce nullable objects to {} before destructuring
Default a possibly-null object to {} before destructuring; property defaults do not protect against destructuring null/undefined.
Bad example
| 1 | public get frontpageTitle(): string { |
| 2 | // this.contents.default may be null/undefined |
| 3 | const { headline = '', headline1 = '' } = this.contents.default; |
| 4 | return headline || headline1; |
| 5 | } |
Explanation (EN)
Destructuring throws 'Cannot destructure property of null' when this.contents.default is null or undefined. The inline defaults (= '') only apply to missing keys on an existing object; they cannot rescue a null/undefined source.
Objašnjenje (HR)
Destrukturiranje baca 'Cannot destructure property of null' kada je this.contents.default null ili undefined. Inline zadane vrijednosti (= '') primjenjuju se samo na nedostajuce kljuceve postojeceg objekta; ne mogu spasiti null/undefined izvor.
Good example
| 1 | public get frontpageTitle(): string { |
| 2 | const { headline = '', headline1 = '' } = this.contents.default ?? {}; |
| 3 | return headline || headline1; |
| 4 | } |
Explanation (EN)
Coalescing to {} guarantees a destructurable object, so the property defaults kick in and the getter returns a safe value instead of throwing. Especially important for entity getters and serializers that run on every request.
Objašnjenje (HR)
Koalesciranje na {} jamci objekt koji se moze destrukturirati, pa zadane vrijednosti svojstava proradi i getter vraca sigurnu vrijednost umjesto bacanja izuzetka. Posebno vazno za getere entiteta i serializatore koji se izvrsavaju pri svakom zahtjevu.
Exceptions / Tradeoffs (EN)
Unnecessary when the source object is statically known to be non-nullable (e.g. a required constructor field assigned a literal). Apply only when the value can realistically be null/undefined.
Iznimke / Tradeoffi (HR)
Nepotrebno kada je izvorni objekt staticki poznat kao ne-nullable (npr. obavezno polje konstruktora s dodijeljenim literalom). Primijeni samo kada vrijednost realno moze biti null/undefined.