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).
Handle the empty/neither case when branching on derived content state
A two-way branch (has text vs not) often hides a third state (no text and no images); handle the empty case explicitly instead of letting it fall into the wrong branch.
Bad example
| 1 | const textOnlyContent = hasText ? serializedHtml : null; |
| 2 |
|
| 3 | // Assumes null => image-only. But null also covers |
| 4 | // the empty case (no text AND no images), which then |
| 5 | // wrongly renders an image placeholder. |
| 6 | if (textOnlyContent === null) { |
| 7 | return <ImagePlaceholder />; |
| 8 | } |
| 9 | return <Content html={textOnlyContent} />; |
Explanation (EN)
Collapsing 'image-only' and 'completely empty' into a single null branch makes the fallback fire for content that has no image, producing a misleading preview.
Objašnjenje (HR)
Spajanje 'samo slika' i 'potpuno prazno' u jednu null granu uzrokuje da se fallback aktivira za sadrzaj bez slike, dajuci pogresan pregled.
Good example
| 1 | const textOnlyContent = hasText ? serializedHtml : null; |
| 2 | const hasImages = fragment.querySelector('img') !== null; |
| 3 |
|
| 4 | if (textOnlyContent !== null) { |
| 5 | return <Content html={textOnlyContent} />; |
| 6 | } |
| 7 | if (hasImages) { |
| 8 | return <ImagePlaceholder />; |
| 9 | } |
| 10 | return null; // no text and no images -> render nothing |
Explanation (EN)
Distinguishing the image-only case from the truly empty case prevents showing an image placeholder when there is no image, keeping the preview honest.
Objašnjenje (HR)
Razlikovanje slucaja 'samo slika' od stvarno praznog slucaja sprjecava prikaz placeholdera za sliku kad slike nema, cuvajuci pregled tocnim.
Exceptions / Tradeoffs (EN)
If the input is contractually guaranteed to always contain text or an image, the neither-case may be unreachable - document that invariant.
Iznimke / Tradeoffi (HR)
Ako je ulaz ugovorno zajamceno da uvijek sadrzi tekst ili sliku, slucaj 'nista' moze biti nedostizan - dokumentiraj taj invarijant.