Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 231
grafpatron Avatar asked Aug 24 '20 09:08

grafpatron


People also ask

Which is better !( A === B or a != B?

In the vast majority of cases, a != b and !( a == b) will have exactly the same behavior, and a != b is almost always clearer.

What is the difference between != And =! In C?

A!= B means " A is not equal to B ". A=! B means "Assign the complement of B to A , and yield the lvalue of A ".

Is a == b the same as b == A?

In Java, A == B and B == A always have the same semantics. In C# (which has operator overloading), there can be a difference if, say, B is an instance of a subclass of the class of A .

What is the difference between equal B and A Equal Equal B?

Answer1: a=b is used for assigning the values (rather then comparison) and a==b is for comparison. Answer4: Equals method compares both type and value of the variable, while == compares value.


Video Answer


4 Answers

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.

like image 116
Andy Turner Avatar answered Sep 30 '22 13:09

Andy Turner


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.

like image 33
Mureinik Avatar answered Sep 30 '22 13:09

Mureinik


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 not 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 :)

like image 20
huntgang99 Avatar answered Sep 30 '22 13:09

huntgang99


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 && !B && !C && !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 "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. Instead you'd have to do someString != null && someString.equals("...") or the Yoda-way of "some specific string".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).

like image 33
m02ph3u5 Avatar answered Sep 30 '22 12:09

m02ph3u5