Boolean Illogic    [ programming/ ]

Why do Java programmers hate the boolean XOR operator?

Is it that they are just generally ignorant about the full-eval boolean operators in general? You know, the operators that look like &, | and ^. Perhaps its that most Java developers are under the impression that they only operate on bits, an are ignorant of the fact that they operate on booleans (and Booleans with that fucking horrible autoboxing/unboxing nonsense.)

I know that 99 time out of 100 we prefer the early-out operators && and || for their efficiency, but a simple ^ can save a hell of a lot of unreadable and less-understandable if-then-else logic. For example, I've just refactored (somebody else's code)

if( check ){
    if( !user.isAnonymousUser() ) doStuff();
}else{
    if( user.isAnonymousUser() ) doStuff();
}

which I find nasty as hell to be sure is doing what it's supposed to, into

if( check ^ user.isAnonymouseUser() ) doStuff();

Much easier to understand, no?




Comments on this entry:

Obviously not, since the version you wrote is not equivalent to the nested ifs ;)

Fri Jan 22 22:50:53 SAST 2010  Keegan -

Yup -- good catch! That'll teach me to just type from memory instead of cutting and pasting the working code, won't it. ;-)

I think the point stand though; it is the original that is so convoluted to understand that I screwed up the really-easy-to-understand.

Fixed now. Thanks for catching me out! :-D

Sat Jan 23 10:44:06 SAST 2010  Mike -

Leave a comment



*Author:
E-mail:
URL:
*Comment:
Remember me?

 

Live Comment Preview