is there a pragmatic reason to use “if (0 == p) ” instead of “if (!p)”?

后端 未结 12 767
难免孤独
难免孤独 2021-02-05 06:18

I\'m inclined to write if statements by using logical negation operator:

if (!p)
    some_code();

Some people around me tend to use explicit co

12条回答
  •  悲哀的现实
    2021-02-05 06:54

    In a really complex conditional, using an explicit == can help make it more readable. Unfortunately, it also opens the door for writing x = 0 instead of x == 0, but you can avoid this by writing 0 == x instead(so that 0 = x will throw an error).

    It could also be habit from other languages where you'd have to cast to a boolean otherwise.

提交回复
热议问题