Skip to content

Prefer Early Return

Suggestion
small-rules/prefer-early-return

Prefer early returns over full-body conditional wrapping.

Diagnostic Messages

preferEarlyReturn
Function body is wrapped in a single conditional without an else branch. This increases nesting depth and cognitive load. Invert the condition and return early: if (!condition) return; then place the main logic at the top level.

Configuration

This rule accepts one options object after the severity.

maximumStatementsOptional

Maximum allowed statement count inside the guarded branch before reporting.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/prefer-early-return": [
"error",
{
"maximumStatements": 1
}
]
}
}

Examples

Function body needs guard clause
function foo() { if (something) { doSomething(); doSomethingElse(); } }
Early return guard clause
function foo() { if (!something) { return; } doSomething(); doSomethingElse(); }