prefer-conditional-expressions
Same as prefer-conditional-expressions in Dart Code Metrics
- Group
- Complexity
- Path
lint/complexity/prefer-conditional-expressions- Default
- Off, opt in
- Origin
- Dart Code Metrics
- Rule
- 325 of 512
Flags an if/else whose branches each reduce to a single return or a
single assignment to the same target.
When both branches differ only in the value they return or assign, a
conditional expression — return c ? a : b; or x = c ? a : b; — says the
same thing in one line. The rule reports only the outermost if of a chain
(whose parent is not an if and whose else is not an else if) where both
branches reduce to a single return, or to a single assignment to the same
variable. Local functions, closures, and labeled statements are included.
Invalid
// Bad: a single assignment or single return in each branch.
String getStatus(bool isActive) {
if (isActive) {
return 'active';
} else {
return 'inactive';
}
}
int getValue(bool hasValue) {
if (hasValue)
return 42;
else
return 0;
}
class Validator {
bool isValid(String input) {
if (input.isNotEmpty) {
return true;
} else {
return false;
}
}
}
void setLabel(bool on) {
String label;
if (on) {
label = 'on';
} else {
label = 'off';
}
print(label);
}String getStatus(bool isActive) { if (isActive) {int getValue(bool hasValue) { if (hasValue) bool isValid(String input) { if (input.isNotEmpty) { String label; if (on) {Valid
// Good: cases dart_code_linter's prefer-conditional-expressions does not flag.
String getStatus(bool isActive) {
return isActive ? 'active' : 'inactive';
}
// Branches are calls, not a single assignment or return.
void printMessage(bool success) {
if (success) {
print('Success!');
} else {
print('Failed!');
}
}
// else-if chains are skipped.
String classify(int code) {
if (code == 0) {
return 'zero';
} else if (code == 1) {
return 'one';
} else {
return 'many';
}
}
// Assignments target different variables.
void assignDifferent(bool flag) {
String x = '';
String y = '';
if (flag) {
x = 'a';
} else {
y = 'b';
}
print('$x$y');
}
// Assignment target is not a simple identifier.
void assignIndex(bool flag, Map<String, int> m) {
if (flag) {
m['a'] = 1;
} else {
m['b'] = 2;
}
}
// Multiple statements per branch.
void processData(bool flag) {
if (flag) {
print('Processing...');
final result = compute();
save(result);
} else {
cleanup();
}
}Configure prefer-conditional-expressions
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"prefer-conditional-expressions": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/prefer-conditional-expressions: <reason>More complexity rules
- prefer-expression-function-bodies
prefer_expression_function_bodiesFlags block function bodies that consist of a single
returnof a value. - prefer-extracting-callbacks
prefer-extracting-callbacksflutterFlags inline block-body callbacks passed to Widget constructors.
- prefer-for-elements-to-map-from-iterable
prefer_for_elements_to_map_fromIterablerecommendedFlags
Map.fromIterable(x, key: ..., value: ...). - prefer-if-elements-to-conditional-expressions
prefer_if_elements_to_conditional_expressionsPrefers collection
ifelements to conditional expressions in list and set literals. - prefer-if-null-operators
prefer_if_null_operatorsrecommendedFlags
x == null ? y : x(and the invertedx != null ? x : y). - prefer-immediate-return
prefer-immediate-returnFlags a local variable that is returned on the immediately following statement.
- prefer-iterable-any
prefer_iterable_anyrecommendedFlags
iterable.where(...).isNotEmpty. - prefer-iterable-every
prefer_iterable_everyrecommendedFlags
iterable.where(predicate).isEmptyand filtered-length comparisons.