Skip to main content

prefer-pushing-conditional-expressions

effort: 2m
teams+

Suggests replacing a conditional expression that has two similar invocations (but with different arguments) with a single invocation that has the conditional expression in one of its arguments.

Example

❌ Bad:

void fn(int left, int right) {
// LINT: The inner expressions differ only by one argument.
// Consider using this conditional expression inside that argument.
final value = left == right ? SomeClass(1, 'hello') : SomeClass(2, 'hello');

// LINT: The inner expressions differ only by one argument.
// Consider using this conditional expression inside that argument.
final value = left == right
? OtherClass(value: 1, other: 'hello')
: OtherClass(value: 2, other: 'hello');
}

✅ Good:

void fn(int left, int right) {
final value = SomeClass(left == right ? 1 : 2, 'hello');

final value = left == right
? OtherClass(value: 1)
: OtherClass(value: 2, other: 'hello');
}