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).
Remove guard clauses made unreachable by an earlier early return
Don't re-check a condition that an earlier guard already rejected and returned on; the later branch is dead code.
Bad example
| 1 | function handleRequest(req, res) { |
| 2 | const { source, items } = req.body; |
| 3 |
|
| 4 | if (!source || !items) { |
| 5 | res.status(400).json({ error: 'Missing source or items.' }); |
| 6 | return; |
| 7 | } |
| 8 |
|
| 9 | // Dead code: !source was already handled above and returned. |
| 10 | if (!source) { |
| 11 | res.status(400).json({ error: 'Missing source.' }); |
| 12 | return; |
| 13 | } |
| 14 |
|
| 15 | process(source, items); |
| 16 | } |
Explanation (EN)
The second `if (!source)` can never be true: any falsy `source` was already caught by the first guard, which returned. The branch is unreachable and just adds noise.
Objašnjenje (HR)
Drugi `if (!source)` nikad ne moze biti istinit: svaki falsy `source` vec je uhvacen u prvom guardu koji je vratio rezultat. Ta grana je nedostizna i samo dodaje sum.
Good example
| 1 | function handleRequest(req, res) { |
| 2 | const { source, items } = req.body; |
| 3 |
|
| 4 | if (!source || !items) { |
| 5 | res.status(400).json({ error: 'Missing source or items.' }); |
| 6 | return; |
| 7 | } |
| 8 |
|
| 9 | process(source, items); |
| 10 | } |
Explanation (EN)
A single guard covers both missing values. Once an early return rejects a falsy value, no later code needs to re-test it.
Objašnjenje (HR)
Jedan guard pokriva obje nedostajuce vrijednosti. Kad early return odbije falsy vrijednost, nijedan kasniji kod ne treba je ponovno provjeravati.
Notes (EN)
When you tighten an early guard, scan the rest of the function for now-unreachable branches that tested the same thing.
Bilješke (HR)
Kad pootrostris early guard, pregledaj ostatak funkcije za sada nedostizne grane koje su provjeravale istu stvar.