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 upNew rule: Disallow mixes of different operators (no-mixed-operators) #566
Comments
feross
added
the
question
label
Jul 13, 2016
This comment has been minimized.
This comment has been minimized.
sotojuan
commented
Jul 14, 2016
|
|
This comment has been minimized.
This comment has been minimized.
nathanph
commented
Jul 14, 2016
•
|
I agree with @sotojuan. Programmers who know what they're doing are probably already parenthesizing their statements. IMO it's naive to assume that someone writing low-level code is more likely to know what they're doing. Even with relatively simple operations parenthesizing helps a ton. // This is difficult to read, not complicated.
const foo = 3 + 4 / 5 + 3 + 5 * 4 + 6
// This is much easier to read.
const foo = 3 + (4 / 5) + 3 + (5 * 4) + 6 |
This comment has been minimized.
This comment has been minimized.
|
Agreed, if mixing: parenthesis. |
This comment has been minimized.
This comment has been minimized.
|
Yup, I still mess this up every now and then haha |
feross
added this to the
standard v9 milestone
Aug 19, 2016
feross
added
enhancement
and removed
question
labels
Aug 19, 2016
This comment has been minimized.
This comment has been minimized.
|
Targeting this for standard v9. |
feross
added a commit
to standard/eslint-config-standard
that referenced
this issue
Feb 8, 2017
This comment has been minimized.
This comment has been minimized.
|
This will be part of the standard v9 beta. We will check all operators except for bitwise operators, since it's very common in low-level modules to omit parens when dealing with binary operators. Maybe we can consider making this stricter in a future version, but I doubt we'll want to. The rule is already very aggressive.
Ecosystem effect is pretty large, but I looked at each failing example and agree that parens ought to be used in each instance to improve clarity. Many of the errors were in my own repos.
|
feross
closed this
Feb 8, 2017
This comment has been minimized.
This comment has been minimized.
|
After fixing repos I control, ecosystem impact looks better, but still substantial.
|
This comment has been minimized.
This comment has been minimized.
|
I do use this pattern a bit: Edit: just read this more closely:
So IIUC the above pattern would not be affected. If so, LGTM Edit 2: lol you wrote "binary" and I read "boolean" but you meant "bitwise" |
This comment has been minimized.
This comment has been minimized.
|
Oh, sorry, I meant bitwise operators, like Personally, I find it confusing to read that line without the parens, even though I know I've written lines like that before. It's harder to visually parse what's going on when reading it, IMO |
This comment has been minimized.
This comment has been minimized.
|
Yeah ok, the parens aren't too bad. I note some irony since in the past I've seen it argued, possibly by me, that explicit semicolons everywhere is as superfluous as explicit parens everywhere. "Just learn these simple operator precedence rules, it's fine. Really!" I realise now that it's a false equivalence though, operator precedence has a great many more rules to remember than ASI. |
This comment has been minimized.
This comment has been minimized.
This is so true. Btw, this is just for the beta release, so worst case if it sucks we can revert before release. |
feross
added a commit
to standard/eslint-config-standard
that referenced
this issue
Feb 9, 2017
This comment has been minimized.
This comment has been minimized.
|
Not being able to use simple math with +, -, /, * without having to force parens seems annoying (i write a lot of math code). Bit-wise ops are confusing though in terms of preference! |
This comment has been minimized.
This comment has been minimized.
|
I can already hear my first grade math teacher yelling at me for not knowing the basic operation order |
This comment has been minimized.
This comment has been minimized.
|
@mafintosh We could turn it off for +, -, /, * since I agree that basic arithmetic order is well-understood. But on longer lines it's still helpful, e.g. return value - Math.floor((value - min) / range) * rangevs. return value - (Math.floor((value - min) / range) * range) |
This comment has been minimized.
This comment has been minimized.
|
I don't personally find that helpful but i get your point. Stuff like this is incredible hard to parse though and i mess up ALL the time so I'd def be okay with enforcing it there
|
This comment has been minimized.
This comment has been minimized.
|
@feross A more common mistake I've seen is something like return value - a + bWhere someone ment return value - (a + b)So just forcing them all over the place for the classic ops makes it harder to question if someone ment to add them or not imo |
This comment has been minimized.
This comment has been minimized.
|
@mafintosh The rule currently doesn't warn about operators that have the exact same precedence, so |
feross commentedJul 13, 2016
http://eslint.org/docs/rules/no-mixed-operators
What do folks think about this rule?
I've always found it super confusing when multiple operators with similar precedence are used without explicit parens. For example:
There are currently a lot of repos that fail when this rule is enabled:
Maybe we can limit it a bit to make it more palatable? There are lots of possible errors. These were pretty common:
The last two are basic algebra order-of-operations that folks usually learn in elementary/middle school, so maybe we can skip warning for those? And the warnings involving
<<happen on modules that are super low-level where we might be able to assume the programmer knows what they're doing.If we just warn when '&&' and '||' are used together without explicit parens, then we get a more reasonable number of warnings:
Curious to get people's thoughts. Is this generally helpful, or just a problem that I have?😉