-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[java] LocalVariableCouldBeFinal on 'size' variable in for loop #1619
Comments
@RocketRider thanks for the report, and sorry for the delay. I'm catching up on some old PMD emails… why can't you move the declaration outside the |
I agree with the proposal. Technically one could extract the declaration outside the for loop, but final int size_1 = loader.size(); final int size_2 = other_list.size(); final int size_3 = outler_list.size(); extracting the declaration outside of the for loop, increases the scope of the declaration ... inviting missuse (and as seen above name clashes). |
Do you need a separate size variable at all? What about this? for (int i = 0; i < loaders.size(); ++i) { |
The idea is to keep the scope of the size variable as small as possible. It does not need to be a separate variable but it is nice if the variable is not visible outside of the loop. The Issue with the last examle is, that it will call "size()" on every iteration. |
The issue is still reproducible under PMD 7.0.0. |
Affects PMD Version: 7.0.0
Rule: LocalVariableCouldBeFinal
Description:
The following line causes a LocalVariableCouldBeFinal warning, but this can't be fixed. As "size" can't be declared final.
The text was updated successfully, but these errors were encountered: