google / google/error-prone

"someDouble = integralA / integralB" may indicate that the coder expects floating-point division

Open
#164 11 comments 0 reactions 0 assignees View on GitHub
migrated Priority-Medium Type-NewCheck
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

_[Original issue](https://code.google.com/p/error-prone/issues/detail?id=164) created by **cpovirk@google.com** on 2013-07-30 at 07:31 PM_

---

(Similarly, "return integralA / integralB" from a double-returning method. Or either of those from a float-returning method. Or "(integralA / integralB) - someDouble." There are a lot of possibilities here, but even a naive check could find a lot of problems.)

The idea is to catch people who write something like "double seconds = millis / 1000" and expect to get a fractional number of seconds. That particular example likely isn't a big deal, as it's often used just for logging. But there are cases in which it can matter. I do see some scary cases in the Google codebase. For example, code will count the number of elements in a collection that match some predicate, and then it will compute "matches / collection.size()" to compare to a threshold. But with integer division, the result is always 0. I collected some of these cases into http://cl/50117990 for viewing inside Google. (There are plenty of other cases that involve time calculations. If I get ambitious, I could pull those out into their own CL.)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.