在IF条件下使用NOT运算符 [英] Using NOT operator in IF conditions

查看:149
本文介绍了在IF条件下使用NOT运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为避免在IF条件下使用NOT运算符以使代码更易读,这是一个很好的做法吗?我听说 if(doSomething())更好,如果(!doSomething())。

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 then if (!doSomething()).

推荐答案

这取决于你想要完成的事情。如果你没有else子句那么 if(!doSomething())似乎没问题。但是,如果你有

It really depends on what you're trying to accomplish. If you have no else clause then if(!doSomething()) seems fine. However, if you have

if(!doSomething()) {
    ...
}
else {
    // do something else
}

我' d可能会反转该逻辑以删除运算符,并使 if 子句更加清晰。

I'd probably reverse that logic to remove the ! operator and make the if clause slightly more clear.

这篇关于在IF条件下使用NOT运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆