question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disallow negated conditions (no-negated-condition)

See original GitHub issue

This rule disallows negated conditions in either of the following:

  • if statements which have an else 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

http://eslint.org/docs/rules/no-negated-condition

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
timoxleycommented, Sep 12, 2016

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 the if. Such an else may require some scrolling to discover what if the else belongs to, and can be easily missed when reading the if.

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
}
2reactions
timoxleycommented, Sep 13, 2016

It works incredibly well at preventing indentation blowout too when resolving it with early exits etc.

https://youtu.be/_wO8toxinoc?t=145

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found