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

address: LiteralsFirstInComparisons PMD rule #368

Conversation

punkratz312
Copy link
Contributor

@punkratz312 punkratz312 commented Oct 14, 2024

@timtebeek
Copy link
Contributor

Thanks again @punkratz312 ! I've got an early flight tomorrow and Friday (mostly) off, so it might be Monday before I get to a review and merge.

@punkratz312
Copy link
Contributor Author

Have a happy flight and enjoy your trip! Thank you for your guidance and support.

…isitor.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
* <a href="https://pmd.github.io/pmd/pmd_rules_java_bestpractices.html#LiteralsFirstInComparisons">Literals First in Comparisons</a>
*
* @param <P> The type of the parent context used for visiting the AST.
*/
@Value
@EqualsAndHashCode(callSuper = false)
public class EqualsAvoidsNullVisitor<P> extends JavaVisitor<P> {
Copy link
Contributor Author

@punkratz312 punkratz312 Oct 17, 2024

Choose a reason for hiding this comment

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

Perhaps LiteralsFirstInComparisonsVisitor would be a better fit to align with the corresponding PMD rule name already in place, as it's no longer just about equality; both equality and comparison are now merely implementation details.

Suggested change
public class EqualsAvoidsNullVisitor<P> extends JavaVisitor<P> {
public class LiteralsFirstInComparisonsVisitor<P> extends JavaVisitor<P> {

Copy link
Contributor

@timtebeek timtebeek Oct 21, 2024

Choose a reason for hiding this comment

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

I'd perhaps hold off on renaming this just yet. While the visitor only appears to be used in this module, the recipe name is well established as part of our Common static analysis issues recipe, which folks have created custom copies off that might break if we similarly rename the recipe. I'll think this over as I go through the review.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes thanks. Its just an idea and of course would be a new PR topic.

github-actions[bot]

This comment was marked as outdated.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 36-38

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 36-38

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 36-38

Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

I like how you've expanded the existing visitor to cover more cases, and by extension the recipe to do more similar replacements without the need for users to adopt any new recipe!

I also appreciate you're looking to apply best practices to the visitor itself, even if those diverge from what's common elsewhere throughout our code base. It'll take some getting used to on both side on what to (not) pick up as part of your work on recipes, as it does lead to more work to review, and at times discussions as to what's the best way forward.

Typically we view recipes and visitors as one complete unit; there's little need to optimize within such a unit, and at times patterns like the four-fold requireNonNull(m.getSelect()) introduced here are a symptom of branching off separate methods, where the nested structure would have no such need. Going forward lets try to minimize such changes, or perhaps propose them first as part of a self-review such that there's no conflicts arising late in review.

I've left a final question about a removed earlier optimization; once that's answered we can merge this work; thanks again!

I753089 added 2 commits October 22, 2024 06:59
…iteralsfirstincomparisons_extend' into EqualsAvoidsNullVisitor-extend-literalsfirstincomparisons_extend
@@ -97,8 +92,8 @@ private static J.Binary literalsFirstInComparisonsNull(J.MethodInvocation m, Exp
m.getPrefix(),
Markers.EMPTY,
requireNonNull(m.getSelect()),
build(J.Binary.Type.Equal).withBefore(SINGLE_SPACE),
firstArgument.withPrefix(SINGLE_SPACE),
JLeftPadded.build(J.Binary.Type.Equal).withBefore(Space.SINGLE_SPACE),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Build maybe okay, but the Space is DRY Space. SPACE

If i need unwanted context i move my mouse and not clutter the code with redundancy, but its a style where most dev´s use full qualified imports.

image

Copy link
Contributor

Choose a reason for hiding this comment

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

Often classes use a mix of the two constants in Space, only one of which has _SPACE in the name for the repetition. I'd prefer then to be consistent with prior use, and i/fwhen we dedice to change that, do so through our enforced patterns in rewrite-recommendations. I hope that context helps!

Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect the idea with the name was to emphasize that the object represents a single space character. The class Space represents any sequence of whitespace and comments, so Space.SINGLE would be a bit ambiguous (a single space, tab, newline, or comment?), so Space.SINGLE_SPACE uses "space" in two different meanings.

@punkratz312 punkratz312 requested a review from timtebeek October 22, 2024 05:04
@punkratz312
Copy link
Contributor Author

Thanks for the feedback and correction. I think we might got it now 👍

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 36-38

@timtebeek timtebeek merged commit 07686d2 into openrewrite:main Oct 22, 2024
2 checks passed
@timtebeek
Copy link
Contributor

Thanks again @punkratz312 !

@punkratz312
Copy link
Contributor Author

Thank you, @timtebeek, it was an honor and a great learning experience. I appreciate the support, guidance, and patience.

@punkratz312 punkratz312 deleted the EqualsAvoidsNullVisitor-extend-literalsfirstincomparisons_extend branch October 23, 2024 18:05
@timtebeek
Copy link
Contributor

You're more than welcome! Your change has just landed in the latest release:
https://github.com/openrewrite/rewrite-static-analysis/releases/tag/v1.19.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants