rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

Recipe to remove underscores in private field names

Open greg-at-moderne opened this issue 6 months ago • 7 comments

What problem are you trying to solve?

Apply modern code standards.

I think it used to be a bit of a common practice to denote private class fields with names which either started with or ended with underscores. While technically allowed, I don't think this is mainstream in Java. Thus it makes sense to have a recipe to rename the fields not to have these _ prefixes/suffixes.

Describe the situation before applying the recipe

public class ParseLocation {
  private String _ruleName;

  public String getRuleName() {
    return _ruleName;
  }
  public void setRuleName(String ruleName) {
    _ruleName = ruleName;
  }
}

Describe the situation after applying the recipe

public class ParseLocation {
  private String ruleName;

  public String getRuleName() {
    return ruleName;
  }
  public void setRuleName(String ruleName) {
    this.ruleName = ruleName;
  }
}

Any additional context

  • When making the changes, beware of name clashes. Even visible in the simple setter example above.
  • For this reason, not marking this is a good-first-issue.

OSS repro

  • leading underscores: https://github.com/apache/pinot/blob/a02253d671d7781acc16b81628e51bd11a3005a0/pinot-spi/src/main/java/org/apache/pinot/spi/utils/builder/TableConfigBuilder.java#L90-L140
  • trailing underscores: https://github.com/rstudio/rstudio/blob/0baa88b0d03238603c1adaa1fe96241cd54b1ba7/src/gwt/src/org/rstudio/studio/client/rmarkdown/events/RenderRmdEvent.java#L120-L129

greg-at-moderne avatar Jul 08 '25 06:07 greg-at-moderne

Hi @greg-at-moderne,

I would like to work on this issue. Could you please assign it to me? Thank you!

jhl221123 avatar Jul 16 '25 13:07 jhl221123

hi @jhl221123 welcome back and thanks for the offer to help! Feel free to open up a draft PR containing just the tests and then we'll assign the issue to you. 🙏🏻

timtebeek avatar Jul 16 '25 13:07 timtebeek

#643 looks like a good start.

greg-at-moderne avatar Jul 22 '25 08:07 greg-at-moderne

Hi, would love to work on this issue , can you assign it to me?

Shiven04123 avatar Aug 01 '25 04:08 Shiven04123

Hi @Shiven04123, thank you for your interest in contributing to this issue.

I'm the one currently assigned to it and have an open pull request at #643. However, it's proving to be a bit more challenging than I first anticipated, which is why it's taking some time. While I do intend to continue my work, I believe the most important thing is that we resolve this issue promptly. So please feel free to build upon my PR if you have better ideas.

jhl221123 avatar Aug 01 '25 05:08 jhl221123

Yes the open pull request is here; if you see any areas for improvement feel free to chime in on the PR:

  • https://github.com/openrewrite/rewrite-static-analysis/pull/643/

timtebeek avatar Aug 22 '25 18:08 timtebeek