简体   繁体   English

!(a==b) 和 a!=b 的区别

[英]Difference between !(a==b) and a!=b

Last week in high school my IT teacher gave us a programm, where one part she wrote interested me a lot.上周在高中时,我的 IT 老师给了我们一个程序,她写的一个部分让我很感兴趣。 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?在 if 语句中,她检查数组的元素是否不为空,因此程序可以继续处理该元素,因此她写道: if (!(array[i] == null) ,这让我感到困惑,因为这种方法你只是更多地使用括号,这使得它不太容易阅读。我将其更改为ìf (array[i] != null)并且它工作得很好。但现在我很困惑,因为作为老师,我认为你知道很多你的主题,所以她为什么用她的方法而不是我的方法有一个很好的理由吗?有什么我不知道的细节吗?

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.很容易忘记清理东西。

In Java, there is no difference between a!=b and !(a==b) .在 Java 中, a!=b!(a==b)之间没有区别。 Choosing the later form is a stylistic choice, which, to be honest, most static analysis tools/IDEs will issue a warning about.选择后一种形式是一种风格选择,老实说,大多数静态分析工具/IDE 都会发出警告。

Your teacher is using other ways to write the if-statement.您的老师正在使用其他方式编写 if 语句。

It doesn't matter if you write你写不重要

if(array[i] != null)

or或者

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

because !因为! defines as not in most programming language.在大多数编程语言中定义为not 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 :)当您对某事感到疑惑时,请务必询问您的老师:)

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:我的建议是一般不要使用if (! (...)) ,因为:

  • 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 && !B && !C && !D .想想!(A || B || C || D) vs !A && !B && !C && !D It might happen that in the former AD 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 ).可能会发生在前一个 AD 中必须被评估然后结果布尔值被翻转的情况,而在后者中它可能足以看到!A评估为 false 并且您已经可以停在那里(因为任何与false成员的连接都会是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) .实际上,编写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)可能更容易掌握。
  • That being said, it's also good to know about Yoda-style writing which is kinda related.话虽如此,了解尤达风格的写作也是一件好事,这有点相关。 Imagine you have String someString;想象一下你有String someString; that might contain a string or might be null .可能包含字符串或可能为null Now imagine you'd like to check if its equal to "some specific string" : someString.equals("some specific string") - yikes, this might produce a NPE because when someString is null there is no method equals to call on it.现在假设您想检查它是否等于"some specific string"someString.equals("some specific string") - 是的,这可能会产生 NPE,因为当someString为 null 时,没有方法equals可以调用它. Instead you'd have to do someString != null && someString.equals("...") or the Yoda-way of "some specific string".equals(someString) .相反,您必须执行someString != null && someString.equals("...")"some specific string".equals(someString)的尤达方式。 There's also other helpers like Objects.equals() .还有其他助手,如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).在这种情况下,我认为这是一个很好的问题(除非她只是告诉你原因)。

There is a difference between a!=b and !(a==b) , it depends on personal preference and what code you're using. a!=b!(a==b)之间有区别,这取决于个人喜好和您使用的代码。 In python a!=b is faster, as other answers have stated.在 python 中a!=b更快,正如其他答案所述。

Actually there is no difference between if (!(array[i] == null) and ìf (array[i] != null)实际上 if (!(array[i] == null) 和 ì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.假设 array[i] 不为空,那么对于 if(!(false)) 中的第一个表达式,最终 !(false) 将为真。

And for the second expression the return result will be directly true.对于第二个表达式,返回结果将直接为真。

So seconnd expression is recommended always.所以总是推荐使用第二个表达式。

The former is not a good idea in some languages like python, In python, for former the better way is前者在某些语言中不是一个好主意,例如 python,在 python 中,前者更好的方法是

if not something==somethingElse

although for latter its perfect, if something != somethingElse ,虽然后者是完美的, 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所以学习前者可能会让你在某些情况下,错误是如此微妙以至于调试变得非常困难,尤其是当你在while循环中使用这个条件while

!= is more efficient than !() . !=!()更有效。 I ran a test in python, and it speaks for itself.我在 python 中运行了一个测试,它不言自明。


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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM