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).
Don't guard against states that cannot occur
Avoid defensive null/branch checks for conditions that are impossible given the surrounding invariants; they add noise and mislead readers.
Bad example
| 1 | const callbackRef = useCallback((node: HTMLDivElement | null) => { |
| 2 | // node can never be null here: the ref object is stable and this |
| 3 | // runs synchronously on mount, yet we branch for a phantom case |
| 4 | if (!node) { |
| 5 | cleanup(); |
| 6 | return; |
| 7 | } |
| 8 | setup(node); |
| 9 | }, []); |
Explanation (EN)
The null branch handles a case that the surrounding invariants make impossible, so it is dead code that implies a possibility readers must reason about.
Objašnjenje (HR)
Null grana obrađuje slučaj koji okolne invarijante čine nemogućim, pa je to mrtav kod koji sugerira mogućnost o kojoj čitatelj mora razmišljati.
Good example
| 1 | const callbackRef = useCallback((node: HTMLDivElement) => { |
| 2 | setup(node); |
| 3 | }, []); |
Explanation (EN)
With the impossible case removed, the code states exactly what can happen and the type no longer advertises a null that never arrives.
Objašnjenje (HR)
Uklanjanjem nemogućeg slučaja kod izražava točno ono što se može dogoditi, a tip više ne oglašava null koji nikad ne dolazi.
Exceptions / Tradeoffs (EN)
Keep the guard if the impossibility isn't actually guaranteed (e.g. the value can change asynchronously or comes from an untyped boundary).
Iznimke / Tradeoffi (HR)
Zadrži zaštitu ako nemogućnost zapravo nije zajamčena (npr. vrijednost se može promijeniti asinkrono ili dolazi s netipiziranog ruba).