Disallow negated conditions (no-negated-condition)
See original GitHub issueThis rule disallows negated conditions in either of the following:
if
statements which have anelse
branch- ternary expressions
Examples of incorrect code for this rule:
/*eslint no-negated-condition: "error"*/
if (!a) {
doSomething();
} else {
doSomethingElse();
}
if (a != b) {
doSomething();
} else {
doSomethingElse();
}
if (a !== b) {
doSomething();
} else {
doSomethingElse();
}
!a ? c : b
Examples of correct code for this rule:
/*eslint no-negated-condition: "error"*/
if (!a) {
doSomething();
}
if (!a) {
doSomething();
} else if (b) {
doSomething();
}
if (a != b) {
doSomething();
}
a ? b : c
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (9 by maintainers)
Top Results From Across the Web
no-negated-condition - Pluggable JavaScript Linter - ESLint
This rule disallows negated conditions in either of the following: if statements which have an else branch; ternary expressions.
Read more >disallow negated conditions (no-negated-condition) - ESLint
Rule Details. This rule disallows negated conditions in either of the following: if statements which have an else branch; ternary expressions.
Read more >No-negated-condition - ESLint - W3cubDocs
Rule Details. This rule disallows negated conditions in either of the following: if statements which have an else branch; ternary expressions.
Read more >ESLint - no-negated-condition Disallows negated conditions.
no -negated-condition. Disallows negated conditions. Negated conditions are more difficult to understand. Code can be made more readable by inverting the ...
Read more >no-negated-condition - Rules - ESLint | Js中文网
Negated conditions are more difficult to understand. Code can be made more readable by inverting the condition instead. Rule Details. This rule disallows...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
While I agree with this for the trivial examples given, I disagree with this when one of the conditional branches is considerably longer than the other. I find it far better to place the shortest branch body first, this gets that condition out of the way and avoids having any “hidden”
else
dangling around at the end of theif
. Such anelse
may require some scrolling to discover whatif
theelse
belongs to, and can be easily missed when reading theif
.The principle I try to follow is to get as much logic “resolved” and out of the way (i.e. don’t need to continue to keep mental track of it) in the fewest possible lines.
e.g.
vs
https://youtu.be/_wO8toxinoc?t=145