Yoda conditions not

JavaScript pattern

Prefer natural language style conditions in favour of Yoda style conditions.


Apply with the Grit CLI
grit apply no_yoda_conditions

Change in if

BEFORE
if (42 == x) {
}
AFTER
if (x == 42) {
}

Change in while

BEFORE
while (42 == x) {}
AFTER
while (x == 42) {}

Change in for

BEFORE
while (42 == x) {}
AFTER
while (x == 42) {}

Leave non literal alone

JAVASCRIPT
if (foo() == x) {
}

Reverse less than

BEFORE
foo(10 < x);
AFTER
foo(x > 10);

Avoid in

JAVASCRIPT
foo('foo' in c);