Using NOT operator in IF conditions

后端 未结 7 1907
遇见更好的自我
遇见更好的自我 2020-12-15 16:10

Is it really a good practice to avoid using NOT operator in IF conditions in order to make your code better readable? I heard the if (doSomething()) is better t

7条回答
  •  庸人自扰
    2020-12-15 16:50

    I never heard of this one before.

    How is

    if (doSomething()) {
    } else {
       // blah
    }
    

    better than

    if (!doSomething()) {
       // blah
    }
    

    The later is more clear and concise.

    Besides the ! operator can appear in complex conditions such as (!a || b). How do you avoid it then?

    Use the ! operator when you need.

提交回复
热议问题