Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
backend ruleP1universalStack: node
readabilitycontrol-flow
Prefer early returns over deep nesting
Early returns keep control flow easy to read and reduce indentation.
Created: Mar 26, 2026
Bad example
Old codets
| 1 | function getLabel(status: string) { |
| 2 | if (status === "ok") { |
| 3 | return "OK"; |
| 4 | } else { |
| 5 | return "Unknown"; |
| 6 | } |
| 7 | } |
Explanation (EN)
Nested branches add noise and make the function harder to scan. As conditions grow, the code becomes harder to maintain.
Objašnjenje (HR)
Ugnijezdene grane stvaraju sum i otezavaju brzo citanje funkcije. Kako uvjeti rastu, kod postaje tezi za odrzavanje.
Good example
New codets
| 1 | function getLabel(status: string) { |
| 2 | if (status !== "ok") return "Unknown"; |
| 3 | return "OK"; |
| 4 | } |
Explanation (EN)
Early returns keep the happy path clear and reduce indentation. The control flow is simpler to extend.
Objašnjenje (HR)
Rani povratak cuva happy path jasnim i smanjuje uvlake. Tok izvrsavanja je jednostavniji za prosirenje.