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.

[New Rule] Reduce conditional expressions to boolean expressions

See original GitHub issue

Explanation

Sometimes conditional expressions can be replaced with boolean ones, like this:

  • a if a else b -> a or b
  • b if a else a -> a and b

This makes the code shorter and removes redundant evaluations of expressions. I think a check to suggest such replacements would fit well in this plugin.

Example

# Bad
x = o.x if o.x else default_x

# Good
x = o.x or default_x

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
SpecLadcommented, Dec 3, 2020

b if a else a is not equivalent to b or a.

0reactions
MartinThomacommented, Feb 28, 2021

As the ternary expressions (a if a else b) are easier for me to understand than the logical expressions (a or b), I will not implement this rule.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Simplify conditional expression (IDE0075) - Microsoft Learn
This style rule concerns simplifying conditional expressions that return a constant value of true or false versus retaining conditional ...
Read more >
Simplifying Boolean Expressions and Conditionals
Just as it is possible to simplify algebraic expressions by using rules like cancellation, commutativity, associativity, distributivity, etc., ...
Read more >
strict-boolean-expressions | typescript-eslint
Disallow certain types in boolean expressions.. Some problems reported by this rule are automatically fixable by the --fix ESLint command line option.
Read more >
Compound Booleans with logical operators - Khan Academy
Since that expression is made entirely of OR operators, the expression is true as long as any of the conditions are true. It...
Read more >
9. Working with Booleans and Conditional Statements
Some rules for rearranging and reasoning about Boolean expressions. An expression that, when evaluated, will result in either true or false. There are...
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