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

Conversation

@tejasbubane
Copy link
Contributor

commented Jun 18, 2019

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
config/default.yml Show resolved Hide resolved

@tejasbubane tejasbubane force-pushed the tejasbubane:float-division branch 3 times, most recently from 2e9cf2e to c7ba4b0 Jun 19, 2019

@tejasbubane

This comment has been minimized.

Copy link
Contributor Author

commented Jun 19, 2019

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

@tejasbubane tejasbubane force-pushed the tejasbubane:float-division branch from c7ba4b0 to a1712bc Jun 20, 2019

@tejasbubane tejasbubane referenced this pull request Jun 20, 2019

@tejasbubane tejasbubane force-pushed the tejasbubane:float-division branch from a1712bc to 1c48371 Jun 20, 2019

@bbatsov

This comment has been minimized.

Copy link
Collaborator

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

This comment has been minimized.

Copy link
Collaborator

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

Update a style guide for "Float Division"
This PR updates a style guide for "Float Division".

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

It is an update based on the following argument.
rubocop-hq/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

This comment has been minimized.

Copy link
Member

commented Jun 22, 2019

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

bbatsov added a commit to rubocop-hq/ruby-style-guide that referenced this pull request Jun 22, 2019

Update a style guide for "Float Division"
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-hq/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 tejasbubane force-pushed the tejasbubane:float-division branch from 1c48371 to 6f2dc54 Jun 24, 2019

@tejasbubane

This comment has been minimized.

Copy link
Contributor Author

commented Jun 24, 2019

@koic PR updated. Changed default style to single_coerce.

config/default.yml Outdated Show resolved Hide resolved

@tejasbubane tejasbubane force-pushed the tejasbubane:float-division branch from 6f2dc54 to c5708ae Jun 24, 2019

@tejasbubane tejasbubane force-pushed the tejasbubane:float-division branch from c5708ae to 8165649 Jun 24, 2019

@koic

koic approved these changes Jun 24, 2019

Copy link
Member

left a comment

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

@bbatsov bbatsov merged commit 7e5c2b2 into rubocop-hq:master Jun 25, 2019

22 checks passed

ci/circleci: cc-setup Your tests passed on CircleCI!
Details
ci/circleci: cc-upload-coverage Your tests passed on CircleCI!
Details
ci/circleci: documentation-checks Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-rubocop Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.3-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.3-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.3-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-spec Your tests passed on CircleCI!
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details

@tejasbubane tejasbubane deleted the tejasbubane:float-division branch Jun 25, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.