比较值时使用NOT或更好吗?

时间:2021-07-02 09:59:45

Is it better to use NOT or to use <> when comparing values in VBScript?
is this:

在比较VBScript中的值时,使用NOT或使用<>是否更好?这是:


If NOT value1 = value2 Then

or this:


If value1 <> value2 Then

better?

EDIT: Here is my counterargument.
When looking to logically negate a Boolean value you would use the NOT operator, so this is correct:

编辑:这是我的反驳。在寻找逻辑上否定布尔值时,您将使用NOT运算符,因此这是正确的:


 If NOT boolValue1 Then

and when a comparison is made in the case of the first example a Boolean value is returned. either the values are equal True, or they are not False. So using the NOT operator would be appropriate, because you are logically negating a Boolean value.

当在第一个例子的情况下进行比较时,返回一个布尔值。要么值等于True,要么它们不是False。因此,使用NOT运算符是合适的,因为您在逻辑上否定了布尔值。

For readability placing the comparison in parenthesis would probably help.

为了便于阅读,将比较放在括号中可能会有所帮助。

4 个解决方案

#1


40  

The latter (<>), because the meaning of the former isn't clear unless you have a perfect understanding of the order of operations as it applies to the Not and = operators: a subtlety which is easy to miss.

后者(<>),因为前者的含义不明确,除非你完全理解操作的顺序,因为它适用于Not和=运算符:一个容易错过的微妙之处。

#2


1  

Agreed, code readability is very important for others, but more importantly yourself. Imagine how difficult it would be to understand the first example in comparison to the second.

同意,代码可读性对其他人来说非常重要,但更重要的是你自己。想象一下,与第二个例子相比,理解第一个例子是多么困难。

If code takes more than a few seconds to read (understand), perhaps there is a better way to write it. In this case, the second way.

如果代码需要花费几秒钟的时间来阅读(理解),也许有更好的方法来编写代码。在这种情况下,第二种方式。

#3


1  

Because "not ... =" is two operations and "<>" is only one, it is faster to use "<>".

Here is a quick experiment to prove it:

因为“not ... =”是两个操作而“<>”只有一个,所以使用“<>”会更快。这是一个快速实验来证明它:

StartTime = Timer
For x = 1 to 100000000
   If 4 <> 3 Then
   End if
Next
WScript.echo Timer-StartTime

StartTime = Timer
For x = 1 to 100000000
   If Not (4 = 3) Then
   End if
Next
WScript.echo Timer-StartTime

The results I get on my machine:

我在机器上得到的结果:

4.783203
5.552734

#4


0  

The second example would be the one to go with, not just for readability, but because of the fact that in the first example, If NOT value1 would return a boolean value to be compared against value2. IOW, you need to rewrite that example as

第二个例子是与之相关的,不仅仅是为了可读性,而是因为在第一个例子中,If NOT value1将返回一个布尔值以与value2进行比较。 IOW,你需要重写那个例子

If NOT (value1 = value2)

which just makes the use of the NOT keyword pointless.

这使得NOT关键字的使用毫无意义。

#1


40  

The latter (<>), because the meaning of the former isn't clear unless you have a perfect understanding of the order of operations as it applies to the Not and = operators: a subtlety which is easy to miss.

后者(<>),因为前者的含义不明确,除非你完全理解操作的顺序,因为它适用于Not和=运算符:一个容易错过的微妙之处。

#2


1  

Agreed, code readability is very important for others, but more importantly yourself. Imagine how difficult it would be to understand the first example in comparison to the second.

同意,代码可读性对其他人来说非常重要,但更重要的是你自己。想象一下,与第二个例子相比,理解第一个例子是多么困难。

If code takes more than a few seconds to read (understand), perhaps there is a better way to write it. In this case, the second way.

如果代码需要花费几秒钟的时间来阅读(理解),也许有更好的方法来编写代码。在这种情况下,第二种方式。

#3


1  

Because "not ... =" is two operations and "<>" is only one, it is faster to use "<>".

Here is a quick experiment to prove it:

因为“not ... =”是两个操作而“<>”只有一个,所以使用“<>”会更快。这是一个快速实验来证明它:

StartTime = Timer
For x = 1 to 100000000
   If 4 <> 3 Then
   End if
Next
WScript.echo Timer-StartTime

StartTime = Timer
For x = 1 to 100000000
   If Not (4 = 3) Then
   End if
Next
WScript.echo Timer-StartTime

The results I get on my machine:

我在机器上得到的结果:

4.783203
5.552734

#4


0  

The second example would be the one to go with, not just for readability, but because of the fact that in the first example, If NOT value1 would return a boolean value to be compared against value2. IOW, you need to rewrite that example as

第二个例子是与之相关的,不仅仅是为了可读性,而是因为在第一个例子中,If NOT value1将返回一个布尔值以与value2进行比较。 IOW,你需要重写那个例子

If NOT (value1 = value2)

which just makes the use of the NOT keyword pointless.

这使得NOT关键字的使用毫无意义。