standardrb/standard

Enable Style/UnlessLogicalOperators

aliismayilov opened this issue · 4 comments

This issue is meant to open a discussion if Style/UnlessLogicalOperators should be enabled in standard config.

The link to the cop: https://docs.rubocop.org/rubocop/cops_style.html#styleunlesslogicaloperators

Whenever I stumble upon a line that uses multiple conditions with unless, I tend to rewrite it locally with if so that I can fully understand the conditions before moving on. In our shared codebase at work, some of us are OK with it and some of us are not. This especially becomes cumbersome when guard clause is involved.

When I work I think in English, but it's very hard for me to mentally parse the following code:

return unless ripe? && eaten? || rotten?

I would vote for forbid_logical_operators style, which would force the above code to become:

if ripe? && eaten?
  return
end

return unless ripe?

but I would be OK with forbid_mixed_logical_operators as well:

return unless ripe? && eaten?
return unless rotten?
searls commented

Whoops, my apologies, I tagged the wrong issue when fixing standardrb/standard-performance#11

Reopening

searls commented

I am amenable to forbid_mixed_logical_operators but I definitely use unless in combination with conditions often. Curious @camilopayan's thoughts here

To be honest, I find unless to be pretty hard to quickly parse regardless of what condition there is, and prefer if statements. I actually parse it as if !(condition) and yea that gets hard with multiple conditions.

Either way, taking on the standard ethos of valuing code clarity above other concerns, I do think that putting in forbid_mixed_logical_operators and encouraging devs to separating out their conditionals seems helpful in that regard.

Let's enable that, then, if you've got a PR cool, just tag me and I'll package it up in the next release in a few weeks.

Closed by #577