Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upDisallow negated conditions (no-negated-condition) #618
Comments
feross
added
the
enhancement
label
Sep 11, 2016
feross
added this to the
standard v9 milestone
Sep 11, 2016
This comment has been minimized.
This comment has been minimized.
|
Probably too disruptive for a rule that isn't automatically fixable:
|
This comment has been minimized.
This comment has been minimized.
|
It would be fun to take a stab at implementing fixing of this rule, it should be technically possible but maybe a bit complicated. I've been thinking of trying to put some work into getting higher fixable coverage. Producing a table of the rules we use and which are currently fixable or not would be awesome. Hopefully I'll get some time for this soon :) |
This comment has been minimized.
This comment has been minimized.
|
@LinusU Nice, that would be greatly useful! |
This comment has been minimized.
This comment has been minimized.
|
Maybe I'm not understanding that example, but what is the benefit of this? if (!a) {
doSomething();
} else if (b) {
doSomething(); // is this meant to be doSomethingElse
}Is not the same as if (a != b) {
doSomething();
} else {
doSomethingElse();
} |
This comment has been minimized.
This comment has been minimized.
|
Yeah -- the examples aren't meant to be the same. They're just examples of valid and invalid code copied from the eslint docs. Basically, this boils down to: Prefer statements that aren't needlessly negated. For example, this is worse: if (!a) {
notA()
} else {
isA()
}Is strictly worse than this: if (a) {
isA()
} else {
notA()
} |
This comment has been minimized.
This comment has been minimized.
|
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" 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. if (a) {
// some lengthy logic, ignore content
for (let i = 0; i < items.length; i++) {
result += items[i]
}
for (let i = 0, token; (token = tokens[i]); i++) {
const token = tokens[i]
result += token
}
items.forEach((item) => {
result += item
})
return result
} else {
console.log(a) // what if does this belong to again?
}vs if (!a) {
console.log(a) // easy to see what conditional this belongs to. else is now done & out of the way.
} else {
// some lengthy logic, ignore content
for (let i = 0; i < items.length; i++) {
result += items[i]
}
for (let i = 0, token; (token = tokens[i]); i++) {
const token = tokens[i]
result += token
}
items.forEach((item) => {
result += item
})
return result
} |
This comment has been minimized.
This comment has been minimized.
|
@timoxley Fair enough. I'll close this. |
feross
closed this
Sep 12, 2016
This comment has been minimized.
This comment has been minimized.
Amen, I wonder if we can capture this more often in standard. |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
reywood
commented
Jan 30, 2018
|
An alternative solution to the lengthy if (a) {
doSomethingWithA(a);
} else {
console.log(a);
}
function doSomethingWithA(a) {
// some lengthy logic, ignore content
for (let i = 0; i < items.length; i++) {
result += items[i]
}
for (let i = 0, token; (token = tokens[i]); i++) {
const token = tokens[i]
result += token
}
items.forEach((item) => {
result += item
})
return result
}Some find this preferable to inverting the conditional. |
feross commentedSep 11, 2016
This rule disallows negated conditions in either of the following:
ifstatements which have anelsebranchExamples of incorrect code for this rule:
Examples of correct code for this rule:
http://eslint.org/docs/rules/no-negated-condition