Skip to content

[java] Explain the existence of AvoidFinalLocalVariable in it's description#1482

Merged
adangel merged 2 commits into
pmd:masterfrom
krichter722:avoid-final-local-var-doc
Jun 30, 2019
Merged

[java] Explain the existence of AvoidFinalLocalVariable in it's description#1482
adangel merged 2 commits into
pmd:masterfrom
krichter722:avoid-final-local-var-doc

Conversation

@krichter722

Copy link
Copy Markdown

Before submitting a PR, please check that:

  • The PR is submitted against master. The PMD team will merge back to support branches as needed.
  • ./mvnw clean verify passes. This will build and test PMD, execute PMD and checkstyle rules. Check this for more info

PR Description:
Rather than being a generally useful rule, AvoidFinalLocalVariable contradicts a good code style in most cases and only serves to avoid a special case (local literals) which it doesn't distinguish.

@ghost

ghost commented Nov 26, 2018

Copy link
Copy Markdown
1 Message
📖 This changeset introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 887 violations, 2 errors and 2 configuration errors.
Full report
This changeset introduces 0 new violations and 0 new errors,
removes 0 violations and 2 errors. Full report

Generated by 🚫 Danger

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR!
Would you mind, changing the description into multiple lines? Note: You do not need to use a CDATA here, just use multiple lines, so that I don't need to scroll horizontally 😄

I had another look at the rule - I can only guess, what the intention of it is: Instead of defining a constant as a final local variable, it should rather be defined as a static final field on the class level (that's how you usually define a constant in java). This of course assumes, that any final local variable initialized with a (constant) literal is actually a constant and has a useful meaning outside the scope of the method.
Maybe the rule was created to avoid duplicated definitions of the same constants in different methods, thus to foster reuse of a constant across methods?

The constant theory seems to be correct, e.g. https://sourceforge.net/p/pmd/bugs/790/ was arguing that final String foo = "foo"; should be defined as a private static final String FOO = "foo";.
Note: The rule is now 11 years old and initially flagged any final local variable without exceptions (14574cf#diff-0a0aaf0e7641329403dac0315535d3bdR415)

@oowekyala

Copy link
Copy Markdown
Member

If the intent is really to extract a constant as a class variable, then the rule should be called ConstantValueMayBeAField, or AvoidDuplicatingLiterals, or something like that. The fact that the local var is final has nothing to do with it, so the name "AvoidFinalLocalVariable" is not adapted. I'd argue for this rule to be deprecated, and some other rule to take its place that does what the rule was intended for, with a relevant name.

@oowekyala

Copy link
Copy Markdown
Member

In fact we already have an AvoidDuplicateLiterals rule in category errorprone (which applies only to string literals). If a constant is not duplicated, then it can stay local to a method without problem: it's probably only relevant to the method, and assuming otherwise would be prone to false positives. Because of this I don't see a reason to keep AvoidFinalLocalVariable anymore.

@adangel

adangel commented Dec 11, 2018

Copy link
Copy Markdown
Member

Refs #811

We should probably also mention, that there is a contradictory rule LocalVariableCouldBeFinal. Having both enabled won't work: One tells, to not use final, the other tells to use final...

I'll try to summarize, what we need to do on master (for 6.11.0):

  • Make sure, we describe, why the rule is not useful.
  • Also mention LocalVariableCouldBeFinal
  • And maybe mention AvoidDuplicateLiterals
  • Deprecate the rule

And then let's remove the rule in 7.0.0.

@adangel adangel added the is:WIP For PRs that are not fully ready, or issues that are actively being tackled label Dec 11, 2018
@adangel adangel added this to the 6.16.0 milestone May 25, 2019
@adangel adangel removed the is:WIP For PRs that are not fully ready, or issues that are actively being tackled label Jun 29, 2019
@adangel adangel merged commit 74f36bf into pmd:master Jun 30, 2019
@krichter722 krichter722 deleted the avoid-final-local-var-doc branch January 9, 2022 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants