Opened 10 years ago
Closed 10 years ago
#11447 closed defect (fixed)
recent coding style changes
Reported by: | bastiK | Owned by: | team |
---|---|---|---|
Priority: | normal | Milestone: | 15.05 |
Component: | Core | Version: | |
Keywords: | Cc: |
Description
A lot of work has been done recently to improve the overall code quality by using automatic code scanning tools. The vast majority of those changes I fully support, but there are some minor things I would like to discuss / reconsider.
- It is wrong to use
Utils.equalsEpsilon
in@Override equals
as it violates the principle that equal objects must have the same hash code (e.g. inLineElemStyle
) - It is not always an error to use "==" for floating point comparison, sometimes it is intended. For example in
StyleCache
, with equalsEpsilon it becomes a gamble that the code works correctly. I could construct a corner case example where it would throw an AssertionError. (similar problem inGeoPropertyIndex
, I think) - Why do you change
x == 0.0
toDouble.doubleToRawLongBits(x) == 0
? First of all, it looks silly.
The performance improvement (if any) should be negligible. Next, it also changes the semantics. E.g. inExpressionFactory.Functions.divided_by(x, ..., y)
, before dividingx / y
, it checks ify
is 0.0 or -0.0. Now it only checks 0.0. (same problem inGeometry
)
I can fix these things myself, just like to give a chance to comment. :)
Attachments (0)
Change History (5)
follow-up: 2 comment:1 by , 10 years ago
Milestone: | → 15.05 |
---|
comment:2 by , 10 years ago
Replying to Don-vip:
Replying to bastiK:
- Why do you change
x == 0.0
toDouble.doubleToRawLongBits(x) == 0
? First of all, it looks silly.
The performance improvement (if any) should be negligible. Next, it also changes the semantics. E.g. inExpressionFactory.Functions.divided_by(x, ..., y)
, before dividingx / y
, it checks ify
is 0.0 or -0.0. Now it only checks 0.0. (same problem inGeometry
)I maybe overlooked the bug description:
http://josm.openstreetmap.de/sonar/coding_rules#rule_key=squid%3AS1244
I assumed it would ensure correctness at no performance cost, how do you understand this rule?
This part of the rule, I don't understand.
x == 0
is equivalent to Double.doubleToRawLongBits(x) == 0 || Double.doubleToRawLongBits(x) == 0x8000000000000000L
(where 0x8000000000000000L is the bit pattern for negative zero). I'm not sure, why it should be an improvement to skip the second check. Maybe if someone is using it wrong, then it will be more obvious that it is wrong.
When 0.0 is a magic value for "not initialized", then it does not matter (both works). But for checks like division by zero, "x == 0
" makes more sense to me.
Replying to bastiK:
thanks :)
You're right, I'm going to revert that.
Can you please revert the locations where this is problematic? even if you're not completely sure :)
I maybe overlooked the bug description:
http://josm.openstreetmap.de/sonar/coding_rules#rule_key=squid%3AS1244
I assumed it would ensure correctness at no performance cost, how do you understand this rule?
Thanks :) I'll discuss upcoming changes that could be problematic before committing now (should have done that from the beginning :) )