Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style/IfWithBooleanLiteralBranches: multiple branches readability #11006

Closed
BuonOmo opened this issue Sep 16, 2022 · 1 comment · Fixed by #11007
Closed

Style/IfWithBooleanLiteralBranches: multiple branches readability #11006

BuonOmo opened this issue Sep 16, 2022 · 1 comment · Fixed by #11007

Comments

@BuonOmo
Copy link

BuonOmo commented Sep 16, 2022

Is your feature request related to a problem? Please describe.

The below method would in my opinion loose in readability if it were corrected by rubocop:

      def contains?(rhs, opts = {})
        if Feature::Geometry === rhs
          contains?(BoundingBox.new(@factory).add(rhs))
        elsif rhs.empty?
          true
        elsif empty?
          false
        elsif @min_x > rhs.min_x || @max_x < rhs.max_x || @min_y > rhs.min_y || @max_y < rhs.max_y
          false
        elsif @has_m && rhs.has_m && !opts[:ignore_m] && (@min_m > rhs.min_m || @max_m < rhs.max_m)
          false
        elsif @has_z && rhs.has_z && !opts[:ignore_z] && (@min_z > rhs.min_z || @max_z < rhs.max_z)
          false
        else
          true
        end
      end

source

The branching helps understanding clearly which result is expected for which conditions. And in the doc examples we can see that it is well suited for simple if-else cases.

Describe the solution you'd like

Having an option that allows for multiple branches to stay as it is:

Style/IfWithBooleanLiteralBranches:
  Enabled: true
  MultipleBranches: false

Describe alternatives you've considered

disabling the rule, but this would make me miss the simple if-else which are to be removed.

@koic
Copy link
Member

koic commented Sep 16, 2022

I'm the author of this cop. TBH, I didn't consider this case when I made this. So, I think it's good to allow when multiple elsifs are used instead of the new option. OTOH, if there is only single elsif, elsif will not exist due to autocorrection. So I think the current behavior can be kept. I've opened #11007.

koic added a commit to koic/rubocop that referenced this issue Sep 16, 2022
…teralBranches`

Fixes rubocop#11006.

This PR allows multiple `elsif` for `Style/IfWithBooleanLiteralBranches`.

I think it's good to allow when multiple `elsif`s are used instead of a new config option.
OTOH, if there is only single `elsif`, `elsif` will not exist due to autocorrection.
So I think the current behavior can be kept.
bbatsov pushed a commit that referenced this issue Nov 1, 2022
…anches`

Fixes #11006.

This PR allows multiple `elsif` for `Style/IfWithBooleanLiteralBranches`.

I think it's good to allow when multiple `elsif`s are used instead of a new config option.
OTOH, if there is only single `elsif`, `elsif` will not exist due to autocorrection.
So I think the current behavior can be kept.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants