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

Don't consider <=> for yoda condition 👾 #4603

Closed
wants to merge 1 commit into from

Conversation

iGEL
Copy link
Contributor

@iGEL iGEL commented Jul 13, 2017

I believe, the <=> operator should not be considered for the yoda condition. Maybe even .comparison_method? shouldn't consider it.

The main reason is, that this method in difference to all other comparison methods doesn't return a boolean, but -1, 0 or 1. Thus 0 <=> a isn't an alternative for a <=> 0, because the one returns -1 while the other returns 1 (unless a is 0 of course).

I haven't checked, but I'm pretty sure the autocorrect will just exchange the left & right side, which is wrong!


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

I believe, the `<=>` operator should not be considered for the yoda
condition. Maybe `.comparison_method?` shouldn't consider it.

The main reason is, that this method in difference to all other
comparison methods doesn't return a boolean, but `-1`, `0` or `1`.
Thus `0 <=> a` isn't an alternative for `a <=> 0`, because the one
returns `-1` while the other returns `1` (unless `a` is `0` of course).

I haven't checked, but I'm pretty sure the autocorrect will just
exchange the left & right side, which is wrong!
@mikegee
Copy link
Contributor

mikegee commented Jul 13, 2017

Maybe even .comparison_method? shouldn't consider it.

Sounds right to me. What would be the fallout of that change?

@Drenmi
Copy link
Collaborator

Drenmi commented Jul 14, 2017

Maybe even .comparison_method? shouldn't consider it.

Sounds right to me. What would be the fallout of that change?

+1 for this. Did a quick check, and the imact should be very close to zero.

@iGEL iGEL mentioned this pull request Jul 19, 2017
11 tasks
@bbatsov
Copy link
Collaborator

bbatsov commented Aug 8, 2017

Guess #4615 makes this redundant.

@bbatsov bbatsov closed this Aug 8, 2017
iGEL added a commit to iGEL/rubocop that referenced this pull request Aug 24, 2017
I believe, the `<=>` operator should not be considered as a
`.comparison_method?`.

The main reason is, that this method in difference to all other
comparison methods doesn't return a boolean, but `-1`, `0` or `1`.
Thus, many assumptions for comparison methods don't apply, for example,
there isn't an inverse comparison method (like `<` is the inverse of
`>=)`, `* -1` isn't a comparison).

This is an alternative to rubocop#4603 by me.
@iGEL iGEL deleted the yoda_spaceship branch August 24, 2017 08:12
bbatsov pushed a commit that referenced this pull request Aug 24, 2017
I believe, the `<=>` operator should not be considered as a
`.comparison_method?`.

The main reason is, that this method in difference to all other
comparison methods doesn't return a boolean, but `-1`, `0` or `1`.
Thus, many assumptions for comparison methods don't apply, for example,
there isn't an inverse comparison method (like `<` is the inverse of
`>=)`, `* -1` isn't a comparison).

This is an alternative to #4603 by me.
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 this pull request may close these issues.

None yet

4 participants