Keep if clauses side-effect free

Avoid writing if clauses that have side effects:

if (enqueueMessage(message)) {
    ...
}

The only function of an if statement is to test whether a condition is true. It’s not for executing code as a side-effect of the test. One problem with using the return value directly, as in the above, is that the meaning of the returned value is unclear. Does enqueueMessage() return true if the message was enqueued or true if the queue is full? Make it explicit by using a variable:

boolean success = enqueueMessage(message);
if (success) {
    ...
}

The above code reads more like English: “If we were successful, …” Methods that don’t have side-effects are (we hope) named so that their return value is clear, such as isEmpty(). This isn’t only true of boolean-valued methods. This code isn’t very clear: