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

Add new cop Style/FloatDivision #7153

Merged
merged 1 commit into from
Jun 25, 2019
Merged

Conversation

tejasbubane
Copy link
Contributor

StyleGuide: https://rubystyle.guide/#float-division


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).
  • 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.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

config/default.yml Outdated Show resolved Hide resolved
@tejasbubane tejasbubane force-pushed the float-division branch 3 times, most recently from 2e9cf2e to c7ba4b0 Compare June 19, 2019 10:38
@tejasbubane
Copy link
Contributor Author

@bbatsov @koic Added third config option right_coerce and kept left_coerce as default (as per style guide).

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 22, 2019

After some deliberation I agree with @koic that the best default would be something that just warns if you use to_f on both sides. Let's also update the style guide accordingly.

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 22, 2019

See also https://blog.rubystyle.guide/ruby/2019/06/21/float-division.html - unfortunately we got no feedback on it.

koic added a commit to koic/ruby-style-guide that referenced this pull request Jun 22, 2019
This PR updates a style guide for "Float Division".

The original issue for which this style was proposed.
rubocop#628

It is an update based on the following argument.
rubocop/rubocop#7153 (comment)

The following is a quote from the argument about the reason for this change.

> I wanted to show the following option to make a bad case only when both have `to_f`.
>
> ```ruby
> # bad
> a.to_f / b.to_f
>
> # good
> a.to_f / b
> a / b.to_f
> a.fdiv(b)
> ```
>
> Whether `to_f` is used on the left or right depends on the nature of `a`
> or `b` parameter. On the other hand, it is redundant that `to_f` is used
> in both. At that time, I think it is better to let users choose whether
> to remove the left side `to_f` or the right side `to_f`. This is the
> reason I recommend this option to default.
@koic
Copy link
Member

koic commented Jun 22, 2019

Thanks for your consideration. I opened a PR rubocop/ruby-style-guide#767 to rubocop-hq/ruby-style-guide repo.

bbatsov pushed a commit to rubocop/ruby-style-guide that referenced this pull request Jun 22, 2019
This PR updates a style guide for "Float Division".

The original issue for which this style was proposed.
#628

It is an update based on the following argument.
rubocop/rubocop#7153 (comment)

The following is a quote from the argument about the reason for this change.

> I wanted to show the following option to make a bad case only when both have `to_f`.
>
> ```ruby
> # bad
> a.to_f / b.to_f
>
> # good
> a.to_f / b
> a / b.to_f
> a.fdiv(b)
> ```
>
> Whether `to_f` is used on the left or right depends on the nature of `a`
> or `b` parameter. On the other hand, it is redundant that `to_f` is used
> in both. At that time, I think it is better to let users choose whether
> to remove the left side `to_f` or the right side `to_f`. This is the
> reason I recommend this option to default.
CHANGELOG.md Outdated Show resolved Hide resolved
@tejasbubane
Copy link
Contributor Author

@koic PR updated. Changed default style to single_coerce.

config/default.yml Outdated Show resolved Hide resolved
Copy link
Member

@koic koic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Thank you for the many updates!

@bbatsov bbatsov merged commit 7e5c2b2 into rubocop:master Jun 25, 2019
@tejasbubane tejasbubane deleted the float-division branch June 25, 2019 08:27
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.

4 participants