Tomás Ó hÉilidhe said:
The language has comments for a reason. If the following is really too
obscure:
a ^= b;
b ^= a;
a ^= b;
, then replace it with:
a ^= b; /* Method for swapping */
b ^= a; /* the values of two */
a ^= b; /* integer types. */
Your comment is very misleading. You should at least write:
a ^= b; // Method for swapping the
b ^= a; // values of two integers
a ^= b; //
// WARNING: this code is only
// correct when
// &a != &b or a == 0
// so don't lift this code and
// use it in an overload of
// swap for int.
//
// RATIONALE: we use this code
// here instead of std::swap
// because ...
Without the warning, the snippet is dangerous since the code might be
reused; and without the rationale section, any competent maintenance
programmer will replace the snippet by swap(a,b) on first sight.
If a programmer shies away from using code just because they can't
understand it at first glance, then I'd doubt the competentancy of that
programmer.
Of course there's business firms out there that have a policy of "the
dumber the better", but that doesn't mean the entire C++ developer
community should be watered down by it.
There seems to be an assumption here that
a ^= b;
b ^= a;
a ^= b;
is smarter than the "watered down" version
swap( a, b );
I wonder how you came to that conclusion. Is there any advantage of the
obscure 3 line version compared to the self-documenting one-liner? I fail
to see it.
Also for the more general point: choosing the harder to understand over the
equivalent easy is poor style; and since it creates maintenance costs it
needs to be justified by gains somewhere else that offset the costs.
Best
Kai-Uwe Bux