“!(a==b)” 翻译为 “!(a等于b)”,”a!=b” 翻译为 “a不等于b”。

huangapple go评论234阅读模式
英文:

Difference between !(a==b) and a!=b

问题

上周在高中,我的信息技术老师给了我们一个程序,其中她写的一部分让我很感兴趣。
在一个if语句中,她检查了数组的一个元素是否不为null,这样程序就可以处理这个元素,因此她写道:
if (!(array[i] == null),这让我感到困惑,因为用这种方法只多了一个括号,使得阅读变得不那么简单。我把它改成了if (array[i] != null),而且它完美地工作了。但现在我感到困惑,因为作为一名教师,我认为您对自己的学科有很多了解,所以她为什么要使用她的方法而不是我的呢?我是不是有关于这个问题的一些我不知道的细节?

英文:

Last week in high school my IT teacher gave us a programm, where one part she wrote interested me a lot.
In an if-statement, she checked if an element of an array was not null, so the programm could proceed with this element, therefore she wrote:
if (!(array[i] == null), which confused me, because with this method you are just using a bracket more, which makes it less easy to read. I changed it to ìf (array[i] != null) and it worked just fine. But now I am confused, because as a teacher I assume you know a lot of your subject, so could there be a good reason why she used her method over mine? Is there some detail I don't know about?

答案1

得分: 16

这两个表达在语义上没有区别。我会说几乎没有充分的理由去使用前者。

但是现在我有些困惑,因为作为一名教师,我假设您对自己的学科有很多了解,所以她为什么要使用她的方法而不是我的呢?

最好的做法是询问您的老师。关于教师最重要的一点是要记住,他们作为人也有经验、偏见,并且是可犯错误的。

换句话说,她可能过去遇到过某些问题,通过这种方式得到了解决 - 问问她,您可能会学到一些东西。

或者,她可能认为这样更好,但无法明确说明个人偏好之外的强大优势;或者那可能是她学过的方式,她还没有看到强烈的改变需求 - 从中您也可以学到一些东西,因为有多种方式来表达相同的语义。

(我更倾向于使用!=,因为它更整洁 - 括号更少;而且如果语言设计者不希望您使用它,为什么要提供!=呢 - 但这些是我个人的偏好)。

或者,她可能本来想使用!=,但忘记了返回进行修正。有时候很容易忘记清理事物。

英文:

There is no difference in the semantics of the two expressions. I would say there is no good reason to ever write the former.

> But now I am confused, because as a teacher I assume you know a lot of your subject, so could there be a good reason why she used her method over mine?

The best thing to do is to ask your teacher. The most important thing to remember about teachers - as human beings - is that they have experiences, prejudices and are fallible.

In other words, she might have been bitten by some problem in the past which was solved by writing it like this - ask her about it, you might learn something.

Or, she perhaps thinks it's better, and can't articulate a strong benefit over personal preference; or that was the way she learnt, and hasn't seen a strong need to change - you learn something from that, insofar as there is more than one way to articulate the same semantics.

(I have a strong preference for != because it's neater - fewer parentheses; and why would the language designers bother providing != if you weren't intended to use it - but these are my personal preferences).

Or, she maybe meant to use !=, and forgot to go back to fix it. It's easy to forget to clean things up.

答案2

得分: 4

在Java中,a!=b!(a==b)之间没有区别。选择后一种形式是一种风格选择,老实说,大多数静态分析工具/集成开发环境都会发出警告。

英文:

In Java, there is no difference between a!=b and !(a==b). Choosing the later form is a stylistic choice, which, to be honest, most static analysis tools/IDEs will issue a warning about.

答案3

得分: 3

你的老师正在使用其他方式来编写 if 语句。

无论你是这样写

if(array[i] != null)

还是这样写

if(!(array[i] == null))

都没有关系,因为在大多数编程语言中,! 被定义为<b> not </b>。在这种情况下,它们是相等的。

总之,你可以选择任何你喜欢的风格。

希望你理解了,祝你好运!

每当你对某件事感到疑惑时,记得随时问你的老师 “!(a==b)” 翻译为 “!(a等于b)”,”a!=b” 翻译为 “a不等于b”。

英文:

Your teacher is using other ways to write the if-statement.

It doesn't matter if you write

if(array[i] != null)

or

if(!(array[i] == null))

because ! defines as <b> not </b> in most programming language. In this situation both of them are equal.

In conclusion, you can choose whatever style you like.

Hope you understood and good luck!

Always ask your teacher when you wonder about something “!(a==b)” 翻译为 “!(a等于b)”,”a!=b” 翻译为 “a不等于b”。

答案4

得分: 3

其他答案是正确的。对于你的例子来说,没有区别 - 选择你最喜欢的。但我想给出一些选择一种方式而不是其他方式的更多理由。

我建议一般情况下不要使用 if (! (...)),因为:

  • 大多数人不会像这样做。你将不得不阅读和理解其他人编写的代码,其他人也将不得不阅读和理解你的代码。这种怪癖极大地降低了可读性,同事们会对你表示不满。
  • 有些项目可能遵循不允许这样写的编码标准。
  • 考虑执行。对于更复杂的语句,不同的风格可能会产生影响。想想 !(A || B || C || D)!A && !B && !C && !D。在前者中,可能需要评估 A-D,然后翻转结果的布尔值,而在后者中,可能只需看到 !A 求值为假,你就可以停在那里(因为与“假”成员相连的任何连接都将是“假”)。
  • 但也要再次考虑可读性。实际上,写 NOT(is_a_digit OR is_a_letter OR is_a_dot) 可能更容易理解,而不是写
    NOT(is_a_digit) AND NOT(is_a_letter) AND NOT(is_a_dot)
  • 话虽如此,还要了解一下与之有点相关的 Yoda 风格的写法。想象一下你有一个可能包含字符串的 String someString; 或者可能为 null。现在想象一下,你想检查它是否等于 “某个特定字符串”someString.equals(“some specific string”) - 糟糕,这可能会产生 NPE,因为当 someString 为 null 时,没有 equals 方法可供调用。相反,你需要执行 someString != null && someString.equals(“...”),或者 Yoda 风格的 “some specific string”.equals(someString)。还有其他辅助方法,如 Objects.equals()

有时额外的括号可以使事情更容易理解,有时则不然。

虽然我不认为“没有愚蠢的问题”,但我认为有时提出一个愚蠢的问题比因为某件事情困扰你或者你错过了一些重要的部分而落后于课程要好。在这种情况下,我认为这是一个好问题(除非她刚刚告诉过你为什么)。

英文:

The other answers are correct. For your example there is no difference - chose what you like the most. But I'd like to give some more reasons to chose one way over the others.

My advice would be not to go for if (! (...)) in general because:

  • Most people wouldn't do it like this. You will have to read and understand code written by others and others will have to read and understand your code. Quirks like that greatly reduce the redability and collegues will frown upon you.
  • Some projects might follow a coding standard that doesn't allow this.
  • Think about the execution. For more complex statements the different style might have an impact. Think of !(A || B || C || D) vs !A &amp;&amp; !B &amp;&amp; !C &amp;&amp; !D. It might happen that in the former A-D have to be evaluated and then the resulting boolean is flipped wheras in the latter it might be enough to see that !A evaluates to false and you can already stop there (because any conjunction with a false member will be false).
  • But also think about readability again. Actually it might be easier to grasp writing NOT(is_a_digit OR is_a_letter OR is_a_dot) instead of
    NOT(is_a_digit) AND NOT(is_a_letter) AND NOT(is_a_dot).
  • That being said, it's also good to know about Yoda-style writing which is kinda related. Imagine you have String someString; that might contain a string or might be null. Now imagine you'd like to check if its equal to &quot;some specific string&quot;: someString.equals(&quot;some specific string&quot;) - yikes, this might produce a NPE because when someString is null there is no method equals to call on it. Instead you'd have to do someString != null &amp;&amp; someString.equals(&quot;...&quot;) or the Yoda-way of &quot;some specific string&quot;.equals(someString). There's also other helpers like Objects.equals().

Sometimes extra parenthesis can make things easier to grasp and sometimes they don't.

While I don't agree that "there are no stupid question" I'd say that sometimes it's better to ask a dumb question than falling behind in the lessons because something nags at you or you missed some important bit. In this case I'd consider this a good question (unless she just told you why).

答案5

得分: 0

a!=b!(a==b)之间是有区别的,这取决于个人偏好和你所使用的代码。在Python中,a!=b更快,正如其他答案所述。

英文:

There is a difference between a!=b and !(a==b), it depends on personal preference and what code you're using. In python a!=b is faster, as other answers have stated.

答案6

得分: -1

实际上,if (!(array[i] == null) 和 if (array[i] != null) 之间没有区别。

在这种情况下,我们可以想象一种情景。假设 array[i] 不为 null,那么在 if(!(false)) 内部的第一个表达式中,最终 !(false) 会变为 true。

而对于第二个表达式,返回结果将直接为 true。

因此,建议始终使用第二个表达式。

英文:

Actually there is no difference between if (!(array[i] == null) and ìf (array[i] != null)

In this case we can imagine a scenario. Lets say array[i] is not null, then for the first expression inside if(!(false)) , eventually !(false) will be true.

And for the second expression the return result will be directly true.

So seconnd expression is recommended always.

答案7

得分: -2

前者在某些语言(比如Python)中不是一个好主意,
在Python中,对于前者,更好的方法是

if not something==somethingElse

尽管对于后者,if something != somethingElse是完美的,

因此,学习前者可能会让你在某些情况下陷入困境,其中bug非常微妙,导致调试变得非常困难,特别是当你在while循环中使用这个条件时。

英文:

The former is not a good idea in some languages like python,
In python, for former the better way is

if not something==somethingElse

although for latter its perfect, if something != somethingElse,

So learning the former might get you in some situations where the bug is so subtle that debugging gets really hard, especially when you use this condition in while loop

答案8

得分: -2

!=!() 更高效。我在Python中进行了一个测试,效果不言而喻。


(not a and not b and not c and not d) 10.07692837715149 

(not (a and b and c and d)) 11.208963394165039

英文:

!= is more efficient than !(). I ran a test in python, and it speaks for itself.


(not a and not b and not c and not d) 10.07692837715149 

(not (a and b and c and d)) 11.208963394165039

huangapple
  • 本文由 发表于 2020年8月24日 17:23:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63558168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定