質問

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?

役に立ちましたか?

解決

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.

他のヒント

Adding parenthesis when it's already obvious how the code will be executed is generally bad. Whether or not something is obvious is a matter of opinion.

Otherwise they're generally good.

For your example it makes sense to add them (to me), although this would eliminate the need for them:

float w = 3 * View.WIDTH / 4

or:

float w = View.WIDTH * 3 / 4
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top