Is there a difference between i==0 and 0==i?

后端 未结 8 1405
我在风中等你
我在风中等你 2020-11-27 22:51

First code:

  if(i==0) {// do instructions here}

Second code:

  if(0==i) { // do instructions here }

What

8条回答
  •  孤独总比滥情好
    2020-11-27 23:18

    Functionally, there is no difference.
    Some developers prefer writing the second format to avoid assignment typos(in case you miss a =), so that compiler warns of the typo.
    The second is famously known as Yoda Condition.

    Yoda Condition

    I say there is no difference because, you cannot guard yourself against every minuscule detail and rely on compiler to cry out aloud for you.If you intend to write a == you should expect yourself to write a == and not a =.
    Using the second format just leads to some obscure non-readable code.
    Also, most of the mainstream compilers warn of the assignment instead of equality typo by emitting an warning once you enable all the warnings(which you should anyways).

提交回复
热议问题