Reputation: 645
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?
Upvotes: 7
Views: 1144
Reputation: 19
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.
Upvotes: 0
Reputation: 147
!=
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
Upvotes: -2
Reputation: 61
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
Upvotes: -2
Reputation: 3161
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:
!(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
).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)
.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).
Upvotes: 3
Reputation: 140318
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.
Upvotes: 16
Reputation: 11
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.
Upvotes: -1
Reputation: 61
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 :)
Upvotes: 3
Reputation: 311468
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.
Upvotes: 4