Rules / Style / binary-expression-operand-order

binary-expression-operand-order

lint/style/binary-expression-operand-order
recommended
Same as binary-expression-operand-order · Dart Code Metrics

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.

Invalid

example.dartdart
// 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) {}
}
Avoid placing literal or constant operand on the left side of a binary expression
4void compareWithLiterals() {
5 if (5 == x) {}
Avoid placing literal or constant operand on the left side of a binary expression
5 if (5 == x) {}
6 if (null == value) {}
Avoid placing literal or constant operand on the left side of a binary expression
6 if (null == value) {}
7 if (true == isActive) {}
Avoid placing literal or constant operand on the left side of a binary expression
7 if (true == isActive) {}
8 if ("test" == name) {}
Avoid placing literal or constant operand on the left side of a binary expression
11void assertLiteralsFirst() {
12 assert(42 < count);
Avoid placing literal or constant operand on the left side of a binary expression
12 assert(42 < count);
13 assert(0 <= index);
Avoid placing literal or constant operand on the left side of a binary expression
13 assert(0 <= index);
14 assert(100 > percentage);
Avoid placing literal or constant operand on the left side of a binary expression
17bool checkValues(int x, String s, bool flag) {
18 return 10 == x || "hello" == s || false == flag;
Avoid placing literal or constant operand on the left side of a binary expression
17bool checkValues(int x, String s, bool flag) {
18 return 10 == x || "hello" == s || false == flag;
Avoid placing literal or constant operand on the left side of a binary expression
17bool checkValues(int x, String s, bool flag) {
18 return 10 == x || "hello" == s || false == flag;
Avoid placing literal or constant operand on the left side of a binary expression
21void testInequality() {
22 if (5 != x) {}
Avoid placing literal or constant operand on the left side of a binary expression
22 if (5 != x) {}
23 if (null != value) {}
Avoid placing literal or constant operand on the left side of a binary expression
27 bool isValid(int age, String email) {
28 return 18 <= age && "example" == email;
Avoid placing literal or constant operand on the left side of a binary expression
27 bool isValid(int age, String email) {
28 return 18 <= age && "example" == email;
Avoid placing literal or constant operand on the left side of a binary expression
32void whileCondition(int count) {
33 while (0 < count) {
Avoid placing literal or constant operand on the left side of a binary expression
38int getValue() {
39 return 5 == null ? 0 : 1;
Avoid placing literal or constant operand on the left side of a binary expression
42void multipleComparisons(int a, int b, int c) {
43 if (1 < a && 2 < b && 3 < c) {}
Avoid placing literal or constant operand on the left side of a binary expression
42void multipleComparisons(int a, int b, int c) {
43 if (1 < a && 2 < b && 3 < c) {}
Avoid placing literal or constant operand on the left side of a binary expression
42void multipleComparisons(int a, int b, int c) {
43 if (1 < a && 2 < b && 3 < c) {}

Valid

example.dartdart
// 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;
}

How to configure

Set the severity of binary-expression-operand-order in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "binary-expression-operand-order": "error"
      }
    }
  }
}