Skip to content

[java] How to understand "AvoidFinalLocalVariable" and "LocalVariableCouldBeFinal" #3043

Answered by jsotuyod
lonelyma1021 asked this question in Q&A
Discussion options

You must be logged in to vote

@lonelyma1021 these are separate rules. It's up to each user to decide if they apply to their projects or not (and if both are required!).

LocalVariableCouldBeFinal attempts to flag local variables as finals. This has 2 main purposes:

  1. help the compiler / JIT improve performance
  2. minimizing mutability

This last one is, in my opinion, the strongest reason to adopt this rule.

Reassigning parameters / local variables means that knowing the actual value they have at a single point requires looking at the whole code between declaration and that point, taking into account all plausible execution paths (which corresponds to cyclomatic complexity).

If variables are final, you only have to look fo…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by adangel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #979 on January 15, 2021 09:36.