This article is based on the PMD documentation article. See the original article on the PMD site: PMD: Java best practices.
Below are the generally accepted best practices.
Reassigning loop variables can lead to hard-to-find bugs. Prevent or limit how these variables can be changed.
In foreach-loops, configured by the
deny: Report any reassignment of the loop variable in the loop body. This is the default.
allow: Don’t check the loop variable.
firstOnly: Report any reassignments of the loop variable, except as the first statement in the loop body. This is useful if some kind of normalization or clean-up of the value before using is permitted, but any other change of the variable is not.
In for-loops, configured by the
deny: Report any reassignment of the control variable in the loop body. This is the default.
allow: Don’t check the control variable.
skip: Report any reassignments of the control variable, except conditional increments/decrements (
-=). This prevents accidental reassignments or unconditional increments of the control variable.
This rule is defined by the following Java class:
|foreachReassign||deny||how/if foreach control variables may be reassigned||no|
|forReassign||deny||how/if for control variables may be reassigned||no|
ava allows the use of several variables declaration of the same type on one line. However, it can lead to quite messy code. This rule looks for several declarations on the same line.
This rule is defined by the following XPath expression:
|strictMode||false||If true, mark combined declaration even if the declarations are on separate lines.||no|