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).
Order type-guard null checks object-first, then property
In type predicates and boolean guards, check the object is non-null before reaching into its properties, instead of leading with an optional-chained property check.
Bad example
| 1 | const valid = results.filter( |
| 2 | (item): item is PlayUrl => item?.url !== null && item !== null, |
| 3 | ); |
Explanation (EN)
Leading with `item?.url !== null` is confusing and out of order: optional chaining already short-circuits when `item` is null/undefined, so the later `item !== null` is partly redundant, and the predicate reads as if it accesses `.url` before establishing `item` exists. The intent (filter out null entries AND entries with a null url) is obscured.
Objašnjenje (HR)
Pocinjanje s `item?.url !== null` je zbunjujuce i u krivom redoslijedu: optional chaining vec kratko spaja kad je `item` null/undefined, pa je kasniji `item !== null` djelomicno suvisan, a predikat se cita kao da pristupa `.url` prije nego sto je utvrdjeno da `item` postoji. Namjera (izbaciti null unose I unose s null url-om) je zamagljena.
Good example
| 1 | const valid = results.filter( |
| 2 | (item): item is PlayUrl => item !== null && item.url !== null, |
| 3 | ); |
Explanation (EN)
Checking `item !== null` first proves the object exists, then `item.url !== null` reads naturally as a guaranteed-safe property access. The guard now plainly states both conditions in the order the reader's mind follows.
Objašnjenje (HR)
Provjera `item !== null` prvo dokazuje da objekt postoji, a zatim se `item.url !== null` prirodno cita kao zajamceno siguran pristup svojstvu. Guard sada jasno navodi oba uvjeta redoslijedom kojim citatelj razmislja.
Notes (EN)
Applies equally to plain boolean conditions, not just `is` type predicates.
Bilješke (HR)
Vrijedi jednako za obicne booleove uvjete, ne samo za `is` type predikate.