Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Remove the else branch after an early return
When an if branch returns, drop the else and let the following code run at the top level for flatter, clearer flow.
Bad example
| 1 | function open(local: boolean) { |
| 2 | if (local) { |
| 3 | return openLocal(); |
| 4 | } else { |
| 5 | return openRemote(); |
| 6 | } |
| 7 | } |
Explanation (EN)
The else nests a path even though the earlier return already excludes it.
Objašnjenje (HR)
Good example
| 1 | function open(local: boolean) { |
| 2 | if (local) return openLocal(); |
| 3 | return openRemote(); |
| 4 | } |
Explanation (EN)
Both paths preserve the original return behavior.
Objašnjenje (HR)
Notes (EN)
Flatten an else only when every path through the preceding branch already exits the enclosing function. Inspect statements after the conditional and block-scoped declarations.
Exceptions / Tradeoffs (EN)
Do not insert a return solely to remove an else if shared cleanup or subsequent work must still run. Preserve braces where bindings would collide.