Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Found Yoda conditions #18

Open
philipjonsen opened this issue Sep 13, 2023 · 0 comments
Open

Found Yoda conditions #18

philipjonsen opened this issue Sep 13, 2023 · 0 comments

Comments

@philipjonsen
Copy link

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';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant