23 January 2010

Boolean Illogic

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?

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...