views:

10186

answers:

8

I personally like the 'exclusive or' operator when it makes sense in context of boolean checks because of its conciseness. I much prefer to write

if (boolean1 ^ boolean2)
{
  //do it
}

than

if((boolean1 && !boolean2) || (boolean2 && !boolean1))
{
  //do it
}

but I often get confused looks (from other experienced java developers, not just the newbies), and sometimes comments about how it should only be used for bitwise operations. I'm curious as to the best practices others use around the '^' operator.

+43  A: 

What's wrong with '!=' ?

Heh, I guess that would produce the same result.
Dre
This would give the incorrect result for false and false.
Jeff Yates
@Mike F: Good point. Well made. It's late. This is why I don't write software after midnight anymore.
Jeff Yates
@ffpf: Me neither :)
too funny, I've just never thought of that with booleans, and you're the first to ever mention it!
Pete
What!!! You just won!
Gab Royer
What incorrect result? false ^ false => false and false != false => false
Peter Lawrey
*"What's wrong with !="* `bool1 ^ bool2 ^ bool3` makes more logical sense to me than `bool1 != bool2 != bool3`
BlueRaja - Danny Pflughoeft
+1  A: 

I'd think it'd be okay if you commented it.

Dre
Even a short comment like thefollowing should be fine://^ = xor
James A. N. Stauffer
Absolutely. A programmer who cannot directly grasp `XOR` is perhaps in the wrong line of work.
Svante
+1  A: 

If the usage pattern justifies it, why not? While your team doesn't recognize the operator right away, with time they could. Humans learn new words all the time. Why not in programming?

The only caution I might state is that "^" doesn't have the short circuit semantics of your second boolean check. If you really need the short circuit semantics, then a static util method works too.

public static boolean xor(boolean a, boolean b) {
    return (a && !b) || (b && !a);
}
Alan
I don't see any short circuiting possible with xor - you have to know both a and b to evaluate the result.
Thelema
Also, the arguments would be evaluated att call time, so no short-circuiting will happen whatsoever.
erikkallen
+6  A: 

I think you've answered your own question - if you get strange looks from people, it's probably safer to go with the more explicit option.

If you need to comment it, then you're probably better off replacing it with the more verbose version and not making people ask the question in the first place.

Martin
+3  A: 

I recently used an xor in a JavaScript project at work and ended up adding 7 lines of comments to explain what was going on. The justification for using xor in that context was that one of the terms (term1 in the example below) could take on not two but three values: undefined, true or false while the other (term2) could be true or false. I would have had to add an additional check for the undefined cases but with xor, the following was sufficient since the xor forces the first term to be first evaluated as a Boolean, letting undefined get treated as false:

 if (term1 ^ term2) { ...

It was in the end a bit of an overkill but I wanted to keep it in there anyway, as sort of an easter egg.

@Martin: I think this is a good case for what you've already stated:

If you need to comment it, then you're probably better off replacing it with the more verbose version and not making people ask the question in the first place.

Ates Goral
Or, you could have `if ( ((bool) term1) != term2 )`
maxwellb
+3  A: 

I find that I have similar conversations a lot. On the one hand, you have a compact, efficient method of achieving your goal. On the other hand, you have something that the rest of your team might not understand, making it hard to maintain in the future.

My general rule is to ask if the technique being used is something that it is reasonable to expect programmers in general to know. In this case, I think that it is reasonable to expect programmers to know how to use boolean operators, so using xor in an if statement is okay.

As an example of something that wouldn't be okay, take the trick of using xor to swap two variable without using a temp variable. That is a trick that I wouldn't expect everybody to be familiar with, so it wouldn't pass code review.

Dave Tarkowski
A: 

!= is OK to compare two variables. It doesn't work, though, with multiple comparisons.

A: 
if((boolean1 && !boolean2) || (boolean2 && !boolean1)) 
{ 
  //do it 
} 

IMHO this code could be simplified:

if(boolean1 != boolean2) 
{ 
  //do it 
} 
Y--