Skip to main content

prefer-early-return

added in: 1.3.0
⚙️

Warns when an if statement can be transformed to an early return. Early returns allow to reduce the code nesting level, improving the overall readability.

⚙️ Config

Set ignore-if-case (default is false) to ignore if statements that have a case block.

dart_code_metrics:
...
rules:
...
- prefer-early-return:
ignore-if-case: false

Example

❌ Bad:

void someFunction() {
if (_value == 2) { // LINT
print('hello');
print(_value);
}
}

✅ Good:

void correct() {
if (_value != 2) return;

print('hello');
print(_value);
}