Using NOT operator in IF conditions

后端 未结 7 1917
遇见更好的自我
遇见更好的自我 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 17:09

    No, there is absolutely nothing wrong with using the ! operator in if..then..else statements.

    The naming of variables, and in your example, methods is what is important. If you are using:

    if(!isPerson()) { ... } // Nothing wrong with this
    

    However:

    if(!balloons()) { ... } // method is named badly
    

    It all comes down to readability. Always aim for what is the most readable and you won't go wrong. Always try to keep your code continuous as well, for instance, look at Bill the Lizards answer.

提交回复
热议问题