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

Prefer BigDecimal.valueOf(double) over new BigDecimal(double) #394

Merged
merged 3 commits into from
Dec 7, 2022

Conversation

Venorcis
Copy link
Contributor

@Venorcis Venorcis commented Dec 6, 2022

A very nice rule to rewrite new BigDecimal(long/int) to BigDecimal.valueOf(long/int) already existed. As using new BigDecimal(double) is quite discouraged, this PR extends the existing rule to rewrite new BigDecimal(double/long/int) to BigDecimal.valueOf(double/long/int)

@Venorcis Venorcis force-pushed the vkoeman/extend-bigdecimal-rules branch from cc81eb8 to 6f11b4c Compare December 6, 2022 21:26
@github-actions

This comment was marked as outdated.

@rickie rickie requested review from Stephan202 and rickie December 6, 2022 21:28
@Venorcis Venorcis force-pushed the vkoeman/extend-bigdecimal-rules branch from 6f11b4c to 346407b Compare December 6, 2022 21:33
/** Prefer {@link BigDecimal#valueOf(long)} over the associated constructor. */
// XXX: Ideally we'd also rewrite `BigDecimal.valueOf("<some-integer-value>")`, but it doesn't
/** Prefer {@link BigDecimal#valueOf(double)} over the associated constructor. */
// XXX: Ideally we'd also rewrite `new BigDecimal("<some-integer-value>")`, but it doesn't
Copy link
Contributor Author

@Venorcis Venorcis Dec 6, 2022

Choose a reason for hiding this comment

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

think this was meant here; not sure if this is a nice comment to keep around anyway, but I'll leave that up to you 😉

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I wonder whether the comment meant a bit of both: if "<some-integer-value>" represents a value that can be expressed as a long, then passing in a long/int is nicer than a string. (But such a change can't be done using Refaster; it'd require a BugChecker.)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see that BigDecimal.valueOf doesn't exist. So certainly that was not meant 😄

@github-actions
Copy link

github-actions bot commented Dec 6, 2022

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.

@@ -18,6 +18,6 @@ ImmutableSet<BigDecimal> testBigDecimalTen() {
}

ImmutableSet<BigDecimal> testBigDecimalFactoryMethod() {
return ImmutableSet.of(new BigDecimal(0), new BigDecimal(0L));
return ImmutableSet.of(new BigDecimal(2), new BigDecimal(2L), new BigDecimal(2.0));
Copy link
Contributor Author

@Venorcis Venorcis Dec 6, 2022

Choose a reason for hiding this comment

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

felt 2 was a nicer value to use here as there is a rule above to rewrite 0 to ZERO as well

also did not include 2F because there is no explicit BigDecimal(float) constructor, whilst there are separate ones for int, long and double; in this sense you could actually consider dropping the integer valueOfs in the existing tests above because they just map to the valueOf(long) anyway (there is no explicit int version).

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.

Nice one @Venorcis! I'll circle back later for a full review :)

/** Prefer {@link BigDecimal#valueOf(long)} over the associated constructor. */
// XXX: Ideally we'd also rewrite `BigDecimal.valueOf("<some-integer-value>")`, but it doesn't
/** Prefer {@link BigDecimal#valueOf(double)} over the associated constructor. */
// XXX: Ideally we'd also rewrite `new BigDecimal("<some-integer-value>")`, but it doesn't
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I wonder whether the comment meant a bit of both: if "<some-integer-value>" represents a value that can be expressed as a long, then passing in a long/int is nicer than a string. (But such a change can't be done using Refaster; it'd require a BugChecker.)

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.

Added a small commit. Have a meeting now; will finalize review afterwards :)

/** Prefer {@link BigDecimal#valueOf(long)} over the associated constructor. */
// XXX: Ideally we'd also rewrite `BigDecimal.valueOf("<some-integer-value>")`, but it doesn't
/** Prefer {@link BigDecimal#valueOf(double)} over the associated constructor. */
// XXX: Ideally we'd also rewrite `new BigDecimal("<some-integer-value>")`, but it doesn't
Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see that BigDecimal.valueOf doesn't exist. So certainly that was not meant 😄

@github-actions
Copy link

github-actions bot commented Dec 7, 2022

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.

Added one more commit. Suggested commit message:

Prefer `BigDecimal.valueOf(double)` over `new BigDecimal(double)` (#394)

See https://rules.sonarsource.com/java/RSPEC-2111

/** Prefer {@link BigDecimal#valueOf(double)} over the associated constructor. */
// XXX: Ideally we also rewrite `new BigDecimal("<some-integer-value>")` in cases where the
// specified number can be represented as an `int` or `long`, but that requires a custom
// `BugChecker`.
static final class BigDecimalFactoryMethod {
Copy link
Member

Choose a reason for hiding this comment

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

While we're here, let's rename this rule in line with our current naming strategy.

Suggested change
static final class BigDecimalFactoryMethod {
static final class BigDecimalValueOf {

@Stephan202 Stephan202 force-pushed the vkoeman/extend-bigdecimal-rules branch from cc3af0f to dce6db6 Compare December 7, 2022 12:53
@github-actions
Copy link

github-actions bot commented Dec 7, 2022

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 Dec 7, 2022

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 Stephan202 added this to the 0.6.0 milestone Dec 7, 2022
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.

Nice catch @Venorcis 🚀 !

@rickie rickie changed the title Extend the refaster rule for new BigDecimal(...) to doubles Prefer BigDecimal.valueOf(double) over new BigDecimal(double) Dec 7, 2022
@rickie rickie merged commit bc1f204 into master Dec 7, 2022
@rickie rickie deleted the vkoeman/extend-bigdecimal-rules branch December 7, 2022 17:58
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