Skip to main content

prefer-simpler-boolean-expressions

added in: 1.23.0
Pro+

Suggests simplifying various boolean expressions.

Boolean conditions with redundant parts reduce readability and make the code overly complex. Consider avoiding such conditions.

Example

❌ Bad:


void fn(bool flag, bool anotherFlag) {
// LINT: This boolean expression '(a && b) || (!a && c)' can be simplified to 'a ? b : c'.
if (flag && _someMethod() || !flag && _anotherOne()) {}

// LINT: This boolean expression '(a && b) || (a && c)' can be simplified to 'a && (b || c)'.
if (_someMethod() && flag || _someMethod() && anotherFlag) {}

// LINT: This boolean expression '!a || a && b' can be simplified to '!a || b'.
if (!flag || flag && _anotherOne()) {}

// LINT: This boolean expression '(a || b) && (a || c)' can be simplified to 'a || (b && c)'.
if ((flag || _anotherOne()) && (flag || _someMethod())) {}
}

bool _someMethod() => false;

bool _anotherOne() => true;

✅ Good:

void fn(bool flag, bool anotherFlag) {
if (flag ? _someMethod() : _anotherOne()) {}

if (_someMethod() && (flag || anotherFlag)) {}

if (!flag && _anotherOne()) {}

if (flag || (_anotherOne() && _someMethod())) {}
}