4 ms·
While these are neat for things like a programmer's interview question I would be very careful about actually using these in real code, for two reasons: 1. It
by asgeirn 14y ago
While these are neat for things like a programmer's interview question I would be very careful about actually using these in real code, for two reasons:
1. It obfuscates the code for very little benefit. Readability suffers more than performance gains.
The JVM could employ similar tricks under the covers for performance reasons if it detects that this is on the hot path.
2. The code is sensitive to overflow, sign handling, Not-a-Number, and divide-by-zero.
Both these points should be raised by an experienced programmer.
- lucian1900 14y agoI would argue that they're harmful even as interview questions. This sort of thing is never a good idea, so why risk giving the other party the impression that maybe it sometimes is?
- ctdonath 14y agoPerhaps they're useful as interview questions insofar as eliciting whether the applicant understands why it's not a good idea. One does not understand an opposing view unless one can give a convincing argument for that view, then tear it apart. I usually apply this to politics & philosophy, but it applies to engineering too: X is a clever approach, but subject to limitations Y. In this case, understanding that more information exists somewhere than is obvious shows insight ... and articulating the limitations of that concept demonstrates superior knowledge. It's not about swapping numbers, it's about how much information can be crammed into two variables and their metadata.
- lucian1900 14y agoThat doesn't work in interviews, since the candidate would be very reluctant to question the interviewer's task.
- CJefferson 14y agoActually, it's worse than that. In both Java and C/C++, doing this kind of thing actually makes code slower. It only makes sense in assembler. When the compiler can deduce that two variables already in registers have swapped value, it simply relabels the registers, making swap a 0-cost operation. Obviously if are swapping two memory locations, you have to read, but a temporary optimises to: mem1 -> reg1 mem2 -> reg2 reg1 -> mem2 reg2 -> mem1 While a swap trick, if the compiler can't backward-enginner it out has to add a bit of xoring (or adding). gcc actually has an optimisation pass with the specific job of trying to take out this trick. Unfortunately, it can't always work because if you try using these tricks on two pointers to the same memory location, you end up zeroing out the value rather than doing nothing (which a proper temporary would).
- viraptor 14y agoTo add one more evil edge case - if you're using something like libgc in your project and try swapping pointers that way, there's a chance you're going to lose your memory by accident.
- praptak 14y ago> It only makes sense in assembler. Not even this (well, there obviously are exceptions), given the existence of XCHG, BSWAP and similar instructions.
- pm215 14y agoI actually think that even as an interview question the real benefit is in using it as a lead-in to the "do you think this is a good idea?" area of questioning. The xor sequence itself is just another "have you seen this trivia question before?" question, which doesn't really reveal any interesting information. Finding out what the candidate thinks about code readability, whether they know what kind of things a compiler or JVM might do, whether their mental model of assembler-level optimisation is still the "all the world is a Z80 and execution time is proportional to instruction count" flavour or if they are aware of caches, pipelines and so on -- these are all useful. You can also take the "ok, plug in some numbers and show me whether your sequence works" tack to find out whether your candidate can work through doing bitwise logical operations on integers, a sort of bit-flipping variant on fizzbuzz. But "have you seen this before" is so uninteresting that I always tried to make it clear that I really wasn't interested in whether they knew the trick or not, but more in the ensuing discussion.
- praptak 14y agoI have actually passed an interview where a misuse of the swap trick was part of the "what's wrong with this piece of code" question. Maybe this is similar to "have you seen this trivia question before" but reverted: "can you spot this idiotic trick that causes bugs", which I believe is much more valuable.