• Post Reply Bookmark Topic Watch Topic
  • New Topic

Negate conditions demorgan  RSS feed

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want negate this:




Which code should I use:

this:




or this (which is equivalent):


 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are not equivalent, because of operator precedence. The following shows how the ! is distributed over the guard:
Your last solution is different; it is As you see, the parentheses are around another pair. It is quite easy to make small mistakes, so unless performance would be a lot better if using the second form I would use the first form, with the ! around the entire guard.

So let's check out performance. If doThis is true, both will return a result immediately. If not but cond1 is false, both will return a result immediately. If not it will evaluate a.equals(b). I don't see any performance difference there, so I would really use the simple form:
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gosh, yeah I forgot the brackets:



is

either (1)



or (2)




You are right, the first one (1) is better as it is simpler.

thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!