-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Introduce a recommended style for float division #628
Comments
Personally I'd prefer the default style to not be a form that requires Personally it seems like the preference should be to enforce that the left hand receives |
@backus: Unless |
Got burned very recently on this bug/feature. Was wondering if there was a cop that would prevent this. It's very easy to make |
@Drenmi Let's recommend left-hand |
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.
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.
Does a rule that forces me to choose between turning it off or having broken code really make sense? |
Well, that's not really in the scope of the rule - it deals with coercion of integers to floats. |
But It applies to any coercion. The rule does not check that it deals with integers. Understandable given the lack of explicit typing but that does not change the consequences... |
I think you're confusing the guidelines here with the RuboCop cop. The cop obviously can't know the type of the receiver and it might yield false positives, but that has little to do with what the guidelines are intended for. On a related note - when dealing with an external data it's better to use |
I guess you are right. Sorry for the confusion. |
By default, Ruby will perform integer division when both receiver and argument are integers. The way you perform float division on integers is to coerce one or more of them into a float. I propose there be a guideline for which part--receiver or argument--should take the
#to_f
coercion.I am leaning towards both the receiver and the argument being coerced, because:
If you only coerce one, changing the code gets more risky, because you may unwittingly replace the float coerced one with an integer, regressing to integer division.
Choosing one to apply#to_f
to is (as far as I can see) completely arbitrary.Possible styles:
Another option could be to recommend
#fdiv
:WDYT?
The text was updated successfully, but these errors were encountered: