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.

no-extra-parens: Allow brackets in expressions that depend on operator precedence

See original GitHub issue

What rule do you want to change? no-extra-parens

Does this change cause the rule to produce more or fewer warnings? fewer

How will the change be implemented? (New option, new default behavior, etc.)? Either new option or new default behaviour. Option may be less contraversial

Please provide some example code that this change will affect:

const cost = fixed + (monthly * 12);

What does the rule currently do for this code? Warn about the “Unnecessary” parentheses

What will the rule do after it’s changed? No warning

Are you willing to submit a pull request to implement this change? I could try.

The justification for this change comes from the results of our readability experiment that looks for empirical data on how coding constructs affect readability. The code above was included in a series of blind A/B tests where we measured the time taken by developers to read a code snippet and whether they could correctly predict its outcome.

We found a statistically significant improvement in both speed of reading the code and accuracty of prediction when the parentheses were included in the code. Whilst they are technically un-necessary if operator precedence is followed, it seems clear that adding them improves readability.

The full results of the experiment are available here:

https://howreadable.netlify.com/results/#operator-precedence

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mdjermanoviccommented, Dec 19, 2019

Thank you for pointing that option out. I’m confused by the naming of the option, as I’m not sure how the examples shown are binary expressions:

https://eslint.org/docs/rules/no-extra-parens#nestedbinaryexpressions

It’s ‘binary’ as - expression with a binary operator (that requires two operands, unlike unary and ternary operators). I guess it could be confused with bitwise operators which work on the binary representation of the data?

Perhaps it’s worth considering having the option as false by default?

It makes sense to me, as the default settings can be seen as a recommendation, but that would be a breaking change and it might be confusing because the rule by default disallows all parens (except those that are the responsibility of other rules).

1reaction
mdjermanoviccommented, Jan 21, 2020

Just to add, ESLint has also a core rule to basically enforce these parentheses: no-mixed-operators

Read more comments on GitHub >

github_iconTop Results From Across the Web

Operator Precedence - Introduction to Programming in Java
When an expression has two operators with the same precedence, the operators ... You can use parentheses to override the default operator ......
Read more >
c++ - When do extra parentheses have an effect, other than on ...
Extra parentheses change the meaning of a C++ program in the following contexts: preventing argument-dependent name lookup; enabling the comma ...
Read more >
Limited dependence should be placed on operator precedence
The value of a complex expression should only be cast to a type that is narrower and of the same signedness as the...
Read more >
Parentheses and operator precedence - IBM
Expression evaluation is from left to right; parentheses and operator precedence modify this: When parentheses are encountered (other than those that ...
Read more >
Should I fully parenthesize expressions or rely on precedence ...
Good developers strive to write code that is clear and correct. Parentheses in conditionals, even if they are not strictly required, help with...
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