Possible Duplicate:
Is there a difference between i==0 and 0==i?
What's the benefit of the following coding styles , is there any difference between them ?
int i;
// more code
if (i == 0) {...}
vs
if (0 == i) {...}
Thanks
Possible Duplicate:
Is there a difference between i==0 and 0==i?
What's the benefit of the following coding styles , is there any difference between them ?
int i;
// more code
if (i == 0) {...}
vs
if (0 == i) {...}
Thanks
No difference at all.
I've always found the latter example less readable, and I rarely see it, but some folks seem to like it.
No difference, pick one and stick with it for consistency. The (value == variable)
is a relic from older languages where you could accidentally assign a value to a variable in an if (a = 0)
, instead of (a == 0)
They will both turn into (effectively) the same machine instruction, so there won't be any performance difference at all
効率に違いはありませんが、読みやすさのためにこのスタイルが推奨されます。
if (i == 0) {...}
もう1つのバージョンは、ヨーダ記法if (0 == i) {...}
の例であり、プログラミングの誤りと見なされています。リンクからの引用:
「ヨーダ記法」— if(4 == foo)のように、if(variable == constant)の代わりにif(constant == variable)を使用します。「青が空なら」や「背が高いなら男」と言っているようなものだからです。