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

Introduce assorted BigDecimal#signum Refaster rules #812

Merged
merged 6 commits into from
Oct 18, 2023

Conversation

Venorcis
Copy link
Contributor

@Venorcis Venorcis commented Oct 6, 2023

Expands the BigDecimalRules, mainly preventing contrived uses of compareTo(BigDecimal.ZERO) by using signum() instead.
Also adds some rules surrounding the use of signum().

@github-actions
Copy link

github-actions bot commented Oct 6, 2023

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Venorcis Venorcis changed the title Introduce assorted BigDecimal Refaster rules Introduce assorted BigDecimal#signum Refaster rules Oct 6, 2023
Copy link
Member

@rickie rickie left a 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)

@rickie rickie added this to the 0.15.0 milestone Oct 6, 2023
@github-actions
Copy link

github-actions bot commented Oct 6, 2023

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@Stephan202 Stephan202 left a 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;
Copy link
Member

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:

  1. Be more consistent with the "Inclusive" rules.
  2. (IMHO) be easier to interpret (the two mistakes in the current rule set also hint at that).
  3. More clearly communicate that the full range of integers is supported (even though in practice only -1, 0 and 1 will be returned).
  4. Maintain consistency with how Comparators should be used.

Copy link
Member

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.)

Copy link
Contributor Author

@Venorcis Venorcis Oct 7, 2023

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:

image

Copy link
Member

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 🤷

Copy link
Contributor Author

@Venorcis Venorcis Oct 7, 2023

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).

Copy link
Member

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. 😄

Copy link
Member

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?

Copy link
Contributor Author

@Venorcis Venorcis Oct 7, 2023

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 😄

Copy link
Member

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 :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the argument @Venorcis made here. But then you also came up with the multiplication argument that is strong.

Nice discussion 🚀 !

@Stephan202 Stephan202 force-pushed the vkoeman/bigdecimal-signum branch from 368a107 to a798bd3 Compare October 7, 2023 09:30
@github-actions
Copy link

github-actions bot commented Oct 7, 2023

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@github-actions
Copy link

github-actions bot commented Oct 7, 2023

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

1 similar comment
@github-actions
Copy link

github-actions bot commented Oct 7, 2023

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202
Copy link
Member

@rickie this PR changed significantly since you last looked at it; feel free to merge if you're happy with the latest changes.

@rickie rickie self-requested a review October 8, 2023 14:24
@rickie rickie force-pushed the vkoeman/bigdecimal-signum branch from 4a451ee to fd53cbf Compare October 18, 2023 06:23
@sonarcloud
Copy link

sonarcloud bot commented Oct 18, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie rickie merged commit 62077da into master Oct 18, 2023
17 checks passed
@rickie rickie deleted the vkoeman/bigdecimal-signum branch October 18, 2023 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants