Rubocop: Use <= and < instead of > or >= in comparison

Created on 26 Nov 2018  路  10Comments  路  Source: rubocop-hq/rubocop

Hi.
I have tried to search in the docs and on the web for a Rubocop rule to use <= and < instead of > or >= in comparison, but can't seem to find anything about it.

Direction is unordered. However the direction should be consistent across the base.

Example: "ascending"

# bad
a > 2
3 >= b
2 <= c && 4 > c

#good
2 < a
b <= 3
4 < c && c <= 7

Example: "descending"

# bad
2 < a
b <= 3
2 <= c && 4 > c

#good
a > 2
3 >= b
7 >= c && c > 4

It can conflict with YodaCondition however I find it easier/faster to figure out when only using the same operators.

feature request stale

All 10 comments

I'm interested in why you believe comparison in one direction is better than the other, or is the issue simply that which ever way the comparison goes it should be consistent across the code base?

Consider giving value objects a look for this sort of work, since that can encapsulate simple type comparison pretty well :)

Yeah, sorry.
The direction shouldn't matter. Like if you like descending or ascending is up to you.
However the comparison should be consistent across the code base for simplicity.

Updated Example.

This seems like a reasonable thing for Rubocop to support. I think ascending comparisons are more natural coming from a mathematical background.

(@casper91 I think the first two "bad" "descending" examples are already descending.)

Yeah, "ascending" is also my preferred choice.
Updated Example, thanks!

I find that a > 2 is more clear than 2 < a, because the variable is the first thing I read, so cognitively it takes up fewer resources. <variable> <operator> <value> should perhaps be the default.

@ekampp, Rubocop currently enforces that style by default with YodaCondition. This issue is a feature request for another option.

Great, then don't break that with this feature by enforcing this feature in a specific direction.

Right on. This feature would need to be another option folks could opt into.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

This issues been automatically closed due to lack of activity. Feel free to re-open it if you ever come back to it.

Was this page helpful?
0 / 5 - 0 ratings