Assignments within sub-expressions are hard to spot and therefore make the code less readable. Ideally, sub-expressions should not have
side-effects.
Noncompliant Code Example
if ((str = cont.substring(pos1, pos2)).isEmpty()) { // Noncompliant
//...
Compliant Solution
str = cont.substring(pos1, pos2);
if (str.isEmpty()) {
//...
Exceptions
Assignments in while
statement conditions, and assignments enclosed in relational expressions are ignored.
BufferedReader br = new BufferedReader(/* ... */);
String line;
while ((line = br.readLine()) != null) {...}
if ((i = j) >= 1) {...}
Chained assignments, including compound assignments, are ignored.
int i = j = 0;
int k = (j += 1);
result = (bresult = new byte[len]);
See