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).
Normalize external empty/missing values once at the boundary
Convert an external source's inconsistent empty/missing representations into one stable type at the boundary, not in every caller.
Bad example
| 1 | // The source can return '', null, undefined, false, or even the literal 'null'. |
| 2 | // Every consumer re-checks all the variants: |
| 3 | if (img && img !== 'null' && img !== '') showImage(img); |
| 4 | // ...elsewhere... |
| 5 | const hasImg = !!img && img !== 'null' && img !== ''; |
Explanation (EN)
The same set of empty/missing checks is duplicated at every call site. It's easy to forget a variant (like the literal string 'null'), and the rules drift over time.
Objašnjenje (HR)
Isti skup provjera za prazno/nedostajuće ponavlja se na svakom mjestu poziva. Lako je zaboraviti neku varijantu (poput literalnog stringa 'null'), a pravila se s vremenom razilaze.
Good example
| 1 | // Normalize once, at the boundary where the value enters the app: |
| 2 | export const coerceEmptyToNull = (value: string | null | undefined | false): string | null => { |
| 3 | if (value == null || value === false || value === '' || value === 'null') return null; |
| 4 | return value; |
| 5 | }; |
| 6 |
|
| 7 | const img = coerceEmptyToNull(rawImg); |
| 8 | if (img) showImage(img); // every consumer sees one stable shape: string | null |
Explanation (EN)
All the messy variants are collapsed into a single string | null at the boundary, so consumers do one simple truthiness check and can't miss an edge case.
Objašnjenje (HR)
Sve neuredne varijante svode se na jedan string | null na granici, pa potrošači rade jednu jednostavnu provjeru istinitosti i ne mogu propustiti rubni slučaj.