Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The conditional block shown in this compliant solution executes only executes when a is equal to b:

Code Block
bgColor#ccccff
public void f(boolean a, boolean b) {
  if (a == b) {
    /* ... */
  }
}

...

When the assignment of b to a is unintended, this conditional block is now executed only when a is equal to b and flag is true:

Code Block
bgColor#ccccff
public void f(boolean a, boolean b, boolean flag) {
  while ( (a == b) && flag ) {
    /* ... */
  }
}

...

The use of the assignment operator in controlling conditional expressions frequently indicates programmer error and can result in unexpected behavior.

ExceptionallyAs an exception to this guideline, it is permitted to use the assignment operator in conditional expressions when the assignment is not the controlling expression (that is, the assignment is a subexpression), as shown in the following compliant solution:

Code Block
bgColor#ccccff
public void assignNocontrol(BufferedReader reader)
   throws throws IOException{
  String line;
  while ((line = reader.readLine()) != null) {
    // ... workWork with line
  }
}

Bibliography

...