-
Notifications
You must be signed in to change notification settings - Fork 39
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 assorted BigDecimal#signum
Refaster rules
#812
Conversation
Looks good. No mutations were possible for these changes. |
BigDecimal
Refaster rulesBigDecimal#signum
Refaster rules
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a commit with some suggestions :).
Suggested commit message (repeating title):
Introduce assorted `BigDecimal#signum` Refaster rules (#812)
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BigDecimalRules.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BigDecimalRules.java
Show resolved
Hide resolved
Looks good. No mutations were possible for these changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W.r.t. the incorrect cases: I have on a TODO list somewhere the idea to generate unit tests from these Refaster rules, so that this kind of thing would be automatically flagged.
Rebased and added two commits; lemme know what you think.
|
||
@AfterTemplate | ||
boolean after(BigDecimal value) { | ||
return value.signum() == -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of == 1
and == -1
, shall we settle on > 0
and < 0
respectively? That would:
- Be more consistent with the "Inclusive" rules.
- (IMHO) be easier to interpret (the two mistakes in the current rule set also hint at that).
- More clearly communicate that the full range of integers is supported (even though in practice only
-1
,0
and1
will be returned). - Maintain consistency with how
Comparator
s should be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(^ With this change we can also further collapse the rules using @AfterTemplate
.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of == 1 and == -1, shall we settle on > 0 and < 0 respectively?
IMO == 1
and == -1
are more readable as meaning 'positive sign' and 'negative sign'. The signum
function is also mathematically defined like that (unlike comparators), which Java follows:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair, but Java's type system still maps those numbers onto larger domain, with the alternative making clear how such won't-happen output would be treated. I.e., a trade-off between mathematical fact and programming pragmatics 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if the type should be leading; such reasoning could be extrapolated to mean you should be prepared for e.g. Collection#size
returning something negative 😛 Perhaps this discussion signals that either < 0
or == -1
should be acceptable, but that other variants are not (and ofc. we need to pick a default of how to rewrite those here).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
such reasoning could be extrapolated to mean you should be prepared for e.g.
Collection#size
returning something negative
Luckily we can side step that particular case ;)
Perhaps this discussion signals that either
< 0
or== -1
should be acceptable, but that other variants are not (and ofc. we need to pick a default of how to rewrite those here).
Indeed, we could also drop some of the cases from the @BeforeTemplate
body. Downside of that is that we won't "clean up" mixed-use code-bases. (And it would still require agreement on what the @AfterTemplate
should contain 😅)
Perhaps this is a case where a flag could help, though right now Refaster doesn't support that (but the idea was also floated in #654 and #655), while a BugChecker
approach would be way more work.
Maybe I'm too a-mathematical on this point; will think about it. Also interested in @rickie's thoughts on this.
NB: Whatever we settle on: we can do the same for {Integer,Long}#signum
. 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upon further reflection, the signum is often multiplied; an operation that crucially depends on the [-1, 0, 1] domain. That's an argument in favour of just embracing that fact. If we do, however, it does make sense to me that we always compare to these values, so also in favour of the >= 0
and <= 0
alternatives proposed earlier.
I pushed a PR in that direction. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That aligns with my original proposal, so SGTM 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NB: Whatever we settle on: we can do the same for
{Integer,Long}#signum
. 😄
I filed #822 for this :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BigDecimalRules.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BigDecimalRules.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BigDecimalRules.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BigDecimalRules.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BigDecimalRules.java
Outdated
Show resolved
Hide resolved
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BigDecimalRules.java
Outdated
Show resolved
Hide resolved
368a107
to
a798bd3
Compare
Looks good. No mutations were possible for these changes. |
...ontrib/src/test/resources/tech/picnic/errorprone/refasterrules/BigDecimalRulesTestInput.java
Outdated
Show resolved
Hide resolved
Looks good. No mutations were possible for these changes. |
1 similar comment
Looks good. No mutations were possible for these changes. |
...ntrib/src/test/resources/tech/picnic/errorprone/refasterrules/BigDecimalRulesTestOutput.java
Show resolved
Hide resolved
@rickie this PR changed significantly since you last looked at it; feel free to merge if you're happy with the latest changes. |
4a451ee
to
fd53cbf
Compare
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Looks good. No mutations were possible for these changes. |
Expands the
BigDecimalRules
, mainly preventing contrived uses ofcompareTo(BigDecimal.ZERO)
by usingsignum()
instead.Also adds some rules surrounding the use of
signum()
.