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 ConstantNaming check #794

Merged
merged 18 commits into from
Oct 27, 2024

Conversation

mohamedsamehsalah
Copy link
Contributor

@mohamedsamehsalah mohamedsamehsalah commented Sep 17, 2023

Suggested commit message:

Introduce `ConstantNaming` check (#794)

This check flags static constants that do not follow the upper snake
case naming convention.

While there, introduce the `Flags#getSet` utility method, and use it to
replace `Flags#getList` in relevant places.

@mohamedsamehsalah mohamedsamehsalah linked an issue Sep 17, 2023 that may be closed by this pull request
2 tasks
@github-actions
Copy link

  • Surviving mutants in this change: 5
  • Killed mutants in this change: 13
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 5 13

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Contributor Author

@mohamedsamehsalah mohamedsamehsalah left a comment

Choose a reason for hiding this comment

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

In addition to class variables within the same compilation unit, should we also match on constants imported from other classes ?
Ideally, the checker will suggest the fix in that class 🤔

Which raises the other question, do we really need to suggest a fix for the other classes referencing the constants in this compilation unit ?

@Stephan202 suggested the use of TreeScanner as in this example but I could not seem to make this work 😓

Help appreciated 🙏

pom.xml Outdated Show resolved Hide resolved
@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch from 31a9e4d to 7fff49f Compare September 17, 2023 17:33
@github-actions
Copy link

  • Surviving mutants in this change: 5
  • Killed mutants in this change: 13
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 5 13

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.

Tnx for working on this @mohamedsamehsalah; it'll allow for some nice cleanup I expect. Did leave some comments based on a quick skim :)

@Stephan202
Copy link
Member

Stephan202 commented Sep 17, 2023

(I didn't check the comments before reviewing; sorry.)

In addition to class variables within the same compilation unit, should we also match on constants imported from other classes ?
Ideally, the checker will suggest the fix in that class 🤔

Error Prone doesn't support this; for cases like that OpenRewrite would be better suited. (But for now, let's just focus on the private fields.)

@Stephan202 suggested the use of TreeScanner as in this example but I could not seem to make this work 😓

Anywhere we can see the code that doesn't work? Perhaps I can take a look.

@github-actions
Copy link

  • Surviving mutants in this change: 11
  • Killed mutants in this change: 14
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 11 14

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@github-actions
Copy link

  • Surviving mutants in this change: 14
  • Killed mutants in this change: 28
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 14 28

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch from 4acc2bc to ecdccc3 Compare September 23, 2023 20:14
@github-actions
Copy link

  • Surviving mutants in this change: 10
  • Killed mutants in this change: 56
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 8 34
🧟tech.picnic.errorprone.bugpatterns.RefasterMethodParameterOrder 1 20
🧟tech.picnic.errorprone.bugpatterns.RefasterMethodParameterOrder$1 1 2

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch from ecdccc3 to 40a2a6f Compare September 23, 2023 20:21
@github-actions
Copy link

  • Surviving mutants in this change: 8
  • Killed mutants in this change: 34
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 8 34

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@mohamedsamehsalah
Copy link
Contributor Author

@Stephan202 I covered your first pass comments, however, I still could not make use of the TreeScanner. Or in other words, I couldn't find what advantage does the TreeScanner give that the TreeMatcher does not.

AFAIU, the suggested fix builder used in this PR will execute what you are suggesting in this comment:

IIUC this change will suggest many distinct fixes. It might work in practice, but it'll be nicer if there's a single report against the field, which, when applied, renames all references to that Symbol. (That's why offline I suggested using a TreeScanner :)

Happy to hear your thoughts (again? 😅) 🙏

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch from 40a2a6f to 0c94940 Compare September 23, 2023 20:29
@github-actions
Copy link

  • Surviving mutants in this change: 8
  • Killed mutants in this change: 34
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 8 34

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.

@Stephan202 I covered your first pass comments, however, I still could not make use of the TreeScanner. Or in other words, I couldn't find what advantage does the TreeScanner give that the TreeMatcher does not.

Actually, you are using a TreeScanner now, just through a method I didn't know existed: have a close look at SuggestedFixes.renameVariable 👀 😄 This should work!

Before I dive into a more thorough review: would you like to have a crack at resolving the surviving mutants?

if (!isVariableUpperSnakeCase(variableName) && !isVariableNameExcluded(variableName)) {
String replacement = toUpperSnakeCase(variableName);

if (!classVariables.contains(replacement)) {
Copy link
Member

Choose a reason for hiding this comment

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

IIUC, here we avoid renaming a variable if the target name is already declared in the exact same scope. Due to the global search and replace that we do, I suspect that any usage of the target name in this CompilationUnit could cause ambiguity, shadowing or confusion. So for this case, too, a TreeScanner may be employed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

would you like to have a crack at resolving the surviving mutants?

Yes, sir 🖖

IIUC, here we avoid renaming a variable if the target name is already declared in the exact same scope. Due to the global search and replace that we do, I suspect that any usage of the target name in this CompilationUnit could cause ambiguity, shadowing or confusion. So for this case, too, a TreeScanner may be employed.

Will look into this 👀

Thanks.

@github-actions
Copy link

github-actions bot commented Oct 1, 2023

  • Surviving mutants in this change: 4
  • Killed mutants in this change: 39
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 3 37
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName$1 1 2

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 2, 2023

  • Surviving mutants in this change: 3
  • Killed mutants in this change: 36
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 2 34
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName$1 1 2

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch 2 times, most recently from 1c55636 to 12e51ab Compare October 2, 2023 06:53
@github-actions
Copy link

github-actions bot commented Oct 2, 2023

  • Surviving mutants in this change: 3
  • Killed mutants in this change: 36
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 2 34
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName$1 1 2

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Oct 2, 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 4 Code Smells

94.9% 94.9% Coverage
0.0% 0.0% Duplication

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

  • Surviving mutants in this change: 3
  • Killed mutants in this change: 36
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName 2 34
🧟tech.picnic.errorprone.bugpatterns.CanonicalConstantFieldName$1 1 2

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Contributor Author

@mohamedsamehsalah mohamedsamehsalah left a comment

Choose a reason for hiding this comment

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

Sorry for the slow progress on this @Stephan202 , PTAL 🙏

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch from 12e51ab to a7f652e Compare November 25, 2023 20:42
Copy link

  • Surviving mutants in this change: 2
  • Killed mutants in this change: 33
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.LexicographicalAnnotationAttributeListing 1 1
🧟tech.picnic.errorprone.bugpatterns.ConstantNaming 1 22
🎉tech.picnic.errorprone.bugpatterns.ConstantNaming$1 0 9
🎉tech.picnic.errorprone.utils.Flags 0 1

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Inject
ConstantNaming(ErrorProneFlags flags) {
exemptedNames =
Sets.union(DEFAULT_EXEMPTED_NAMES, Flags.getSet(flags, ADDITIONAL_EXEMPTED_NAMES_FLAG))
Copy link
Member

Choose a reason for hiding this comment

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

Here and below: Pitest correctly flags that the Sets#union arguments can be swapped without causing a test failure. That's expected, as set union is a symmetric operation, and this code doesn't care about the created set's iteration order.

@Stephan202 Stephan202 force-pushed the mohamedsamehsalah/396-canonical-constant-naming branch from 94fb2a4 to d7a3938 Compare October 27, 2024 12:20
Copy link

Copy link

  • Surviving mutants in this change: 2
  • Killed mutants in this change: 33
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.LexicographicalAnnotationAttributeListing 1 1
🧟tech.picnic.errorprone.bugpatterns.ConstantNaming 1 22
🎉tech.picnic.errorprone.bugpatterns.ConstantNaming$1 0 9
🎉tech.picnic.errorprone.utils.Flags 0 1

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202 Stephan202 merged commit 21437a4 into master Oct 27, 2024
16 checks passed
@Stephan202 Stephan202 deleted the mohamedsamehsalah/396-canonical-constant-naming branch October 27, 2024 13:24
new TreeScanner<Boolean, @Nullable Void>() {
@Override
public Boolean visitVariable(VariableTree tree, @Nullable Void unused) {
return ASTHelpers.getSymbol(tree).getSimpleName().toString().equals(name)
Copy link
Member

Choose a reason for hiding this comment

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

Can be simplified to

Suggested change
return ASTHelpers.getSymbol(tree).getSimpleName().toString().equals(name)
return ASTHelpers.getSymbol(tree).contentEquals(name);

I noticed a similar discussion here, so decided to create a Refaster rule: #1379.

@ben-manes
Copy link

I'm not sure what your style guide is, but fwiw Google's is popular and does not follow this rule (e.g. logger not LOGGER as this suggests). Of course not an issue as trivial to suppress or disable, just fyi that this may be considered overly broad as broaches into style vs quality.

https://google.github.io/styleguide/javaguide.html#s5.2.4-constant-names

@Stephan202
Copy link
Member

Thanks for sharing @ben-manes! I suppose this one of the (likely few) ways in which inside Picnic we deviate from the Google style guide. In version 0.19.0 we also landed #783, which by default will suggest LOG for SL4FJ loggers. In that case we support an override (though while still going UPPER_SNAKE_CASE) for static loggers). For this one a flag would have to unlock significantly more advanced heuristics, and it'd never be perfect. Indeed disabling may be best for some (most?) users. 🤔

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.

Canonical constant naming
4 participants