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

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

问题描述

避免在 IF 条件下使用 NOT 运算符以提高代码可读性真的是一个好习惯吗?我听说 if (doSomething())if (!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
}

我可能会颠倒这个逻辑以删除 ! 运算符并使 if 子句更加清晰.

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

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

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