在 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()).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果可以选择的话,避免使用 ! 运算符通常不是一个坏主意。一个简单的原因是它可能成为错误的来源,因为有可能忽视它。在某些情况下,更具可读性的是: if(conditionA==false) 。如果你跳过 else 部分,这主要起作用。
无论如何,如果您有 else 块,则不应在 if 条件中使用否定。
除了这样的组合条件:
这里你必须使用某种否定来获得所需的逻辑。
在这种情况下,真正值得思考的是函数或变量的命名。
尝试为它们命名,以便您可以经常在简单的条件下使用它们而无需否定。
在这种情况下,您应该考虑 isNotC() 的逻辑,以及如果有意义的话是否可以用 isC() 方法替换它。
最后,你的例子在可读性方面还有另一个问题,这个问题比是否使用否定的问题更严重:代码的读者是否真的知道 doSomething() 何时返回 true,何时返回 false?
如果它是假的,那么它还是完成了吗?这是一个非常常见的问题,最终导致读者试图找出函数返回值的真正含义。
It is generally not a bad idea to avoid the !-operator if you have the choice. One simple reason is that it can be a source of errors, because it is possible to overlook it. More readable can be: if(conditionA==false) in some cases. This mainly plays a role if you skip the else part.
If you have an else-block anyway you should not use the negation in the if-condition.
Except for composed-conditions like this:
Here you have to use some sort of negation to get the desired logic.
In this case, what really is worth thinking about is the naming of the functions or variables.
Try to name them so you can often use them in simple conditions without negation.
In this case you should think about the logic of isNotC() and if it could be replaced by a method isC() if it makes sense.
Finally your example has another problem when it comes to readability which is even more serious than the question whether to use negation or not: Does the reader of the code really knows when doSomething() returns true and when false?
If it was false was it done anyway? This is a very common problem, which ends in the reader trying to find out what the return values of functions really mean.
这实际上取决于您想要实现的目标。如果您没有 else 子句,那么
if(!doSomething())
似乎没问题。但是,如果您有的话,我可能会反转该逻辑以删除
!
运算符并使if
子句稍微更清晰。It really depends on what you're trying to accomplish. If you have no else clause then
if(!doSomething())
seems fine. However, if you haveI'd probably reverse that logic to remove the
!
operator and make theif
clause slightly more clear.作为一般性陈述,最好使 if 条件尽可能具有可读性。对于您的示例,使用 !没问题。问题是,当事情看起来像
您可能想做类似的事情
时,您的 if 语句会被简化为
它只会使代码更具可读性。如果必须调试,您可以调试 isOk 变量集,而不必挖掘作用域中的变量。它对于处理 NPE 也很有帮助——将代码分解成更简单的块总是好的。
As a general statement, its good to make your if conditionals as readable as possible. For your example, using ! is ok. the problem is when things look like
you might want to do something like
then your if statement is simplified to
It just makes the code more readable. And if you have to debug, you can debug the isOk set of vars instead of having to dig through the variables in scope. It is also helpful for dealing with NPEs -- breaking code out into simpler chunks is always good.
不,在
if..then..else
语句中使用!
运算符绝对没有问题。变量的命名以及您的示例中的方法的命名很重要。如果您使用:
但是:
这一切都取决于可读性。始终以最具可读性为目标,这样就不会出错。始终尝试保持代码的连续性,例如,看看 Bill the Lizards
No, there is absolutely nothing wrong with using the
!
operator inif..then..else
statements.The naming of variables, and in your example, methods is what is important. If you are using:
However:
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.
一般来说, !是一个非常好的、可读的布尔逻辑运算符。没有理由不使用它,除非您通过删除双重否定或应用摩根定律来简化。
或者
根据经验,请保持布尔返回方法的签名易于记忆并符合约定。 @hvgotcodes 提出的场景的问题在于,ab 和 cde 当然一开始就不是非常友好的示例。假设您有一个航班预订应用程序的航班和座位类别。那么预订航班的条件完全可以像
这样完全可读且易于理解的代码。不过,您可以重新定义 Seat 类的布尔逻辑,并将条件改写为此。
从而删除 !运算符(如果它确实困扰您),但您会发现这完全取决于您的布尔方法的含义。
In general, ! is a perfectly good and readable boolean logic operator. No reason not to use it unless you're simplifying by removing double negatives or applying Morgan's law.
or
As a rule of thumb, keep the signature of your boolean return methods mnemonic and in line with convention. The problem with the scenario that @hvgotcodes proposes is that of course a.b and c.d.e are not very friendly examples to begin with. Suppose you have a Flight and a Seat class for a flight booking application. Then the condition for booking a flight could perfectly be something like
This perfectly readable and understandable code. You could re-define your boolean logic for the Seat class and rephrase the condition to this, though.
Thus removing the ! operator if it really bothers you, but you'll see that it all depends on what your boolean methods mean.
尝试这样
它是一样的
try like this
It's same with
我以前从未听说过这个。
How
比
后者更清晰简洁。
除了!运算符可以出现在复杂的条件中,例如 (!a || b)。那你如何避免呢?
使用!需要时操作员。
I never heard of this one before.
How is
better than
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.
我认为何时使用
if (!doSomething())
的一个很好的例子是在 Java 11 之前使用Optional
。Java 11 添加了isEmpty
code>,但在此之前只有isPresent
。您试图从函数中尽早返回的图像(常见的编程最佳实践)将是一种非常常见的做法。或者,如果您确实想避免
if
中的!
,您可以自己引入一个临时变量I think a good example of a case of when to use
if (!doSomething())
would be usingOptional
before Java 11. Java 11 addedisEmpty
, but before that there was onlyisPresent
. Image you are trying to return early from a function ( a common programming best practice)would be a very common practice. Alternatively you could introduce a temporary variable yourself if you really want to avoid the
!
inside theif