noctcore-code-quality/prefer-early-return
Prefer a guard clause over wrapping the whole function body in an
if.
Recommended preset: error · Autofix: no · Suggestions: no · Type information: not needed
When a function’s entire body is wrapped in a single if with no else, the happy path is
needlessly indented and the reader has to hold the condition in their head to the end. Inverting
the condition into an early return keeps the meaningful work at the top level and reads top-to-bottom.
What it flags
Section titled “What it flags”The last statement of a function block that is an if with:
- no
elsebranch, and - a block consequent holding two or more statements.
// the whole body is wrappedfunction handle(x) { if (x) { doA(); doB(); }}// guard clausefunction handle(x) { if (!x) { return; } doA(); doB();}What it does not flag
Section titled “What it does not flag”A single-statement if, an if/else, or an if that is not the final statement is left alone —
those are not body-wraps.
When not to use it
Section titled “When not to use it”If you prefer the wrapped style, or lint a codebase where trailing single-if bodies are idiomatic,
disable it.