binary-expression-operand-order
Same as binary-expression-operand-order in Dart Code Metrics
- Group
- Style
- Path
lint/style/binary-expression-operand-order- Default
- Off, opt in
- Origin
- Dart Code Metrics
- Rule
- 193 of 512
Flags comparison expressions with a literal on the left-hand side.
Writing the constant first — if (0 == count) — is the "Yoda condition"
style that reads backwards; putting the variable first (count == 0) matches
how the comparison is spoken and scanned. The check covers the equality and
relational operators (==, !=, <, >, <=, >=) whenever the left
operand is a literal, with one exception: a null on the left compared
against another literal (as in null == null) is left alone.
Checks each comparison once throughout the program, including parenthesized expressions, initializers, and collections.
Invalid
// Test cases for binary-expression-operand-order rule
// All lines with violations should have an expectation annotation
void compareWithLiterals() {
if (5 == x) {}
if (null == value) {}
if (true == isActive) {}
if ("test" == name) {}
}
void assertLiteralsFirst() {
assert(42 < count);
assert(0 <= index);
assert(100 > percentage);
}
bool checkValues(int x, String s, bool flag) {
return 10 == x || "hello" == s || false == flag;
}
void testInequality() {
if (5 != x) {}
if (null != value) {}
}
class Validator {
bool isValid(int age, String email) {
return 18 <= age && "example" == email;
}
}
void whileCondition(int count) {
while (0 < count) {
count--;
}
}
int getValue() {
return 5 == null ? 0 : 1;
}
void multipleComparisons(int a, int b, int c) {
if (1 < a && 2 < b && 3 < c) {}
}void compareWithLiterals() { if (5 == x) {} if (5 == x) {} if (null == value) {} if (null == value) {} if (true == isActive) {} if (true == isActive) {} if ("test" == name) {}void assertLiteralsFirst() { assert(42 < count); assert(42 < count); assert(0 <= index); assert(0 <= index); assert(100 > percentage);bool checkValues(int x, String s, bool flag) { return 10 == x || "hello" == s || false == flag;bool checkValues(int x, String s, bool flag) { return 10 == x || "hello" == s || false == flag;bool checkValues(int x, String s, bool flag) { return 10 == x || "hello" == s || false == flag;void testInequality() { if (5 != x) {} if (5 != x) {} if (null != value) {} bool isValid(int age, String email) { return 18 <= age && "example" == email; bool isValid(int age, String email) { return 18 <= age && "example" == email;void whileCondition(int count) { while (0 < count) {int getValue() { return 5 == null ? 0 : 1;void multipleComparisons(int a, int b, int c) { if (1 < a && 2 < b && 3 < c) {}void multipleComparisons(int a, int b, int c) { if (1 < a && 2 < b && 3 < c) {}void multipleComparisons(int a, int b, int c) { if (1 < a && 2 < b && 3 < c) {}Valid
// Good cases for binary-expression-operand-order rule
// No violations expected
void compareWithVariables() {
if (x == 5) {}
if (value == null) {}
if (isActive == true) {}
if (name == "test") {}
}
void assertVariablesFirst() {
assert(count > 42);
assert(index >= 0);
assert(percentage < 100);
}
bool checkValues(int x, String s, bool flag) {
return x == 10 || s == "hello" || flag == false;
}
void testInequality() {
if (x != 5) {}
if (value != null) {}
}
class Validator {
bool isValid(int age, String email) {
return age >= 18 && email == "example";
}
}
void whileCondition(int count) {
while (count > 0) {
count--;
}
}
int getValue() {
return null == 5 ? 0 : 1;
}
void multipleComparisons(int a, int b, int c) {
if (a > 1 && b > 2 && c > 3) {}
}
void stringComparisons(String name, String expected) {
if (name == expected) {}
if (name.isEmpty) {}
if (name.isNotEmpty) {}
}
void numericOperations(int x, double y, num z) {
assert(x > 0);
assert(y < 100.5);
assert(z >= 10);
}
bool logicalCombinations(bool a, bool b, int count) {
return a && b || count == 5;
}
final exemptComparisons = [null == 0, null == null, value == 0, 0 + value];
final nestedExemptComparisons = (valid: null == false, other: value < 0);Configure binary-expression-operand-order
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"binary-expression-operand-order": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/binary-expression-operand-order: <reason>More style rules
- boolean-prefixes
boolean_prefixesFlags boolean identifiers that lack a conventional interrogative prefix.
- camel-case-extensions
camel_case_extensionsrecommendedFlags named extensions whose name is not
UpperCamelCase. - camel-case-types
camel_case_typesrecommendedFlags type names that are not
UpperCamelCase. - cast-nullable-to-non-nullable
cast_nullable_to_non_nullableFlags
e as Tcasts of a nullable value to a non-nullable type. - class-members-ordering
class_members_orderingFlags class members declared out of a canonical order.
- combinators-ordering
combinators_orderingFlags
show/hidecombinators whose names are not sorted alphabetically. - conditional-uri-does-not-exist
conditional_uri_does_not_existrecommendedRequire the targets of conditional import and export URIs to exist.
- constant-identifier-names
constant_identifier_namesrecommendedFlags constant identifiers that are not lowerCamelCase.