Search code examples
javaoperator-precedenceparentheses

Are obvious parentheses bad?


In the example:

float w = (View.WIDTH / 4) * 3;

The parentheses do not alter the outcome (right?) so are therefore redundant. Yet I find they make the intention of the code clearer. It is a style faux pas to have such redundant parentheses?


Solution

  • When parentheses do not affect the outcome of an expression you should ask yourself: 'Will adding these parentheses make this expression easier or harder to read?' If the answer is the former, feel free to add them.

    So no - it's not bad in this example, but it can be bad if used excessively.