Closed bkoelman closed 4 years ago
So how would you change the rule?
Remove redundant parentheses in expressions if they do not clarify precedence. Add parentheses in expressions to avoid non-obvious precedence. For example, in nested conditional expressions: overruled || (enabled && active)
, bitwise and shift operations: foo | (bar >> size)
.
I think that's a good balance.
Existing guidance:
Add parentheses around every binary expression, but don’t add parentheses around unary expressions. For example if (!string.IsNullOrEmpty(str) && (str != "new"))
Most developers are unaware that && has higher precedence than ||, which makes expressions like below hard to read:
For other cases I think precedence is obvious for most developers:
Recently the usage of parentheses was added to Visual Studio / .editorconfig:
and to Resharper:
This enables us to be more specific on which cases parentheses are helpful and have automated formatting settings for them.