Skip to content

Found Yoda conditions #18

@philipjonsen

Description

@philipjonsen

DESCRIPTION
Yoda conditions are named so because the literal value of the condition comes first while the variable comes second.

For instance,

if ("red" === color) {
// ...
}
Yoda condition is fixed by switching the literal and variable.

This is called a Yoda condition because it reads as, "if red equals the color", similar to the way the Star Wars character Yoda speaks. Compare to the other way of arranging the operands:

if (color === "red") {
// ...
}
This typically reads, "if the color equals red", which is arguably a more natural way to describe the comparison.

BAD PRACTICE
if ("red" === color) {
// ...
}

if (true == flag) {
// ...
}

if (5 > count) {
// ...
}

if (-1 < str.indexOf(substr)) {
// ...
}
RECOMMENDED
if (color === "red") {
// ...
}

if (flag === true) {
// ...
}

if (count < 5) {
// ...
}

if (str.indexOf(substr) > -1) {
// ...
}

Find it here:

Expected literal to be on the right side of <=
src/UI/Rating/Rating.js

let listingCondition;
if (rating && rating === 5) {
listingCondition = 'Awesome';
} else if (4 <= rating && rating < 5) {
listingCondition = 'Good';
} else if (3 <= rating && rating < 4) {
listingCondition = 'Average';
Expected literal to be on the right side of <=
src/UI/Rating/Rating.js

listingCondition = 'Awesome';

} else if (4 <= rating && rating < 5) {
listingCondition = 'Good';
} else if (3 <= rating && rating < 4) {
listingCondition = 'Average';
} else if (2 <= rating && rating < 3) {
listingCondition = 'Bad';
Expected literal to be on the right side of <=
src/UI/Rating/Rating.js

listingCondition = 'Good';

} else if (3 <= rating && rating < 4) {
listingCondition = 'Average';
} else if (2 <= rating && rating < 3) {
listingCondition = 'Bad';
} else if (rating >= 1) {
listingCondition = 'Terrible';

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions