- Feature Articles
-
CodeSOD
- Most Recent Articles
- Irritants Make Perls
- Crossly Joined
- My Identification
- Mr Number
- intint
- Empty Reasoning
- Zero Competence
- One Month
-
Error'd
- Most Recent Articles
- Not Impossible
- Monkeys
- Killing Time
- Hypersensitive
- Infallabella
- Doubled Daniel
- It Figures
- Three Little Nyms
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
pretty standard, indeed.
Admin
This makes my teeth itch, but if you change it like this, it doesn't:
Admin
I work in mostly C# and TypeScript and our team doesn't need to use bitwise operations often enough to know offhand what the operators are and their implications, so the XOR being there actually makes the code less readable at a glance to us.
That being said, I can't think of a great way to write this condition without an XOR, so if I saw the XOR solution come up in a PR I would probably approve it.
Admin
Like other commenters, I have yet to see the need for logical XOR, so I would fart in the general direction of the business requirements underpinning all those XOR alluded to in the post.
Admin
You can simply use != on two booleans as an xor operator
A project I worked on in C had a lot of functions where you had to pass in either A or B (both were strings), so this was easily validated by
if (!!A != !!B) { }
Admin
Given Booleans, inequality, i.e. bool(text) != bool(otherText), is the same as xor and might be more readable.
Admin
"But I also can't complain too much about this one. I hate it, don't get me wrong" Just shows how low we've sunken ...
Admin
What surprises me most here is that this was "all over the codebase." as soon as I saw the snippet I was thinking about how I'd do this in Python and... I'm note sure I've ever had to (and I write a lot of Python). I've branched across all three cases (and, xor, nor), but it's rare, if ever, that I've wanted to xor two conds without differentiating "both" from "neither." Makes me wonder what else the contractor is doing that makes this come up.
And it's not like this is a Python thing either - C and all its syntactic descendants also have bitwise [and, or, xor, not] but only logical [and, or, not], also without causing any issue. Indeed the motivation for && and || seems to be not correct handling of values other than {0, 1}, but short-circuiting, which doesn't work for xor. And ! because ~1 is not 0 (Many years ago, I was brought up on (classic) VB and I do sometimes see the merit of its choice of -1 for truth.)
Admin
I wouldn't call it a WTF, but in my experience, XOR is rare enough to be flagged as a code smell. I don't remember ever having requirements that rely on either A or B, but not both. Except for code challenges like Advent of Code, but can be somewhat unusual.
Admin
I think I agree with other people, that it isn't a common thing. If it comes up from requirements (so not an algorithm thing), I think the best thing would actually be the long winded thing.
If ((X || Y) && !(X && Y))
Admin
Either article will be wrong for some subset of readers because the correct article depends on one's pronunciation. Presumably you prefer "ex-or" rather than "zor"?
Admin
I've been flipping bits for over 25 years now, and still this code hurts my eyes. Just write it out, please?!?
The problem with XOR usually is that it is thoroughly misunderstood. (b1 XOR b2) XOR b3 will not result in an exclusive OR function (one, and only one of the bits is true)
Admin
Anything that makes you stop and go Eh? is a WTF.
At least re-write this as:
if (cond1 or cond2) and !(cond1 and cond2): # do actual work
Why is this is used in a lot of places?
Admin
Your suggestion of
If ((X || Y) && !(X && Y))
requires more thinking upon looking than is necessary, but I like Tim's suggestion ofif (!!A != !!B) { }
even less, for the same reason. I would writeif ((A && !B) || (B && !A)) { }
because IMHO it is the most understandable to the most people.Admin
Fscking bloody system. I'm logged in and my comment is still "held for moderation". A great start to my morning.
Admin
And xor is not not or or xor and or
Admin
That makes sense if you pronounce it "ex or" and not "zor". It's a "zor" operation, when I say it.
Admin
Indeed. Once you extend XOR beyond just two inputs, it becomes a function to calculate the "even parity" parity bit of the inputs.
(For the folks in the audience who've missed the point: if b1, b2 and b3 are all true, the function given above will be true as well, which is not the understanding of the programmer on the Clapham Omnibus(1) about "exclusive or".)
(1) From the Britishism "the man on the Clapham Omnibus", meaning an average "everyman" type of figure. ("Clapham" == a borough in the southern part of Greater London, and "omnibus" == an obsolete term for "bus")
Admin
As you say, it depends on the pronunciation, but I pronounce it as "exor" (rather than "ex or"), so for me it's definitely a teeth-itcher.
Addendum 2024-12-12 11:40: If I wanted a pronunciation that isn't "exor", these days I'd go for "ixor" or "ksor", i.e. French style. The French do not zedify initial X.
Admin
Dunno about all that. I'd define an itty-bitty function intended to be tagged "inline" (or even "__always_inline" on compilers that support it) that calculates xor, so the if()s look like
if ( xor( A, B ) )
.Admin
I see it a fair bit coming from business requirements where there's a lot of listing every possible option that could be employed. It's not uncommon to hear the four states coming from two bools represented as
If X and Y then display combined option If X but not Y OR Y but not X then display individual option If neither X or Y then do not display any option
This becomes a lot messier and more commin when you're working on a single system that's feature locked for 100+ different customers.
Admin
As an EE, it is definitely not pronounced "zor". In software, XOR is an implementation of what originally was discrete logic "exclusive-or" where it's pronounced x-clusive, not z-clusive.
Admin
The true wtf is the writer of this article not understanding that a boolean xor is the same as boolean unequal
Admin
Subject to the need to normalise truthy/falsy values into actual booleans first, but yes. Shame that K&R (or C89/90, C99, etc.) didn't include
^^
as a logical XOR in the same vein as&&
and||
.Admin
Slow-clapping the opening paragraphs.
Admin
So you'd pronounce " Markov xor 0" as Mark of zorro" ?
Admin
Maybe it's just me, but somehow getting "zor" from a shortening of "exclusive or" is a bigger WTF than anything in the article.
Admin
The only place I can think of for an XOR condition is where the "# do stuff" raises an exception.
Admin
I like that one but also like
as this will short circuit if both are true, and is still understandable (testing if a and b are not both true nor both false), and there can be some cases where such short circuiting can be desirable.
Admin
Which can be simplified one layer to if (!A != !B) { }. although I'm not sure that'd be any less confusing. :)
Admin
It can be ... If A & B have already been normalzed to Booleans from other truthy/falsey types of values. The
!!A
does normalization then no-op double-negation in very few characters. Once it's a common idion in your codebase it becomes pretty obvious.Admin
if (A+B==1)
or if you must worry about truthiness and also want to be clear that at integer addition, if ((int)(bool)A+(int)(bool)B == 1)
Addendum 2024-12-12 17:24: Sorry for typoo
Admin
Programming is communication above all else. You, or one of your colleagues will come by your code in half a year, two years, or if you work in the same business as I do, a decade or two later. Will your code make your brain bleed?
This code made my brain bleed.
Not being one for code golf, let's just think this through. As I understand it, we want to check if one statement is true and only one, so:
bool weHaveBeer = ...; bool weHaveGlasses = ...; if ((weHaveBeer && !weHaveGlasses) || (!weHaveBeer && weHaveGlasses)) runInCirclesScreamAndShout();
You get what's happening here...
If I misunderstood this... well, as I said, it's communication above all... so, then, very "well" communicated code....
And yeah, I'm a c#/Java programmer and this discussion of "python xor not python" just made me an even more convinced such programmer... ;)
Admin
If it weren't for the fact that you're right, I'd argue for a "shor" operation as something that one might perform at the beach. Or not.
Admin
The article pointed out why that isn't safe to rely on: Python is both dynamically typed and loosely typed, so there's no guarantee that the arguments are actually booleans.
The idiomatic way to convert something to a boolean in Python is just
bool(something)
, incidentally - no double bangs required.Admin
Mark-off ksor zeerow.
Admin
No, Python is dynamically typed, but it's generally strictly typed. Other than some implicit conversions for numeric types, Python will raise an exception if you try to use a variable in a place that can't handle the variable's type.
Admin
Not completely true: Perl has logical xor (also known as
^^
).Admin
Sorry, I disagree:
(X && !Y) || (Y && !X) is correct, but I think is harder to parse the intention.
((X || Y) && !(X && Y)) is easier to read as "X or Y and [but] not both", which is the essence of XOR
Admin
Another WTF is Python not having a boolean xor.