by kaddar · 16y ago · 0 comments
If performance is an issue, then use these obfuscating idioms, but only if you are able to abstract them well and maintain performance (as you suggest), or document them completely and concisely.
There is a balance to achieve. This is one example, and arguing the specifics of this one example is not as useful as being able to make judgement calls in the wild, for real, and get most of the close enough to right. It's not exact.
But idioms, per se, should not be rejected out of hand purely because they're unknown to you.
This idiom obfuscates, as you state yourself, because people who aren't comfortable with the language will find it jarring at first. I understand where you are coming from: If everyone knew it, it wouldn't be obfuscating. But I argue that people forget idioms or never learn idioms that don't map to our human explanations rather quickly, so it increases maintenance costs.
I argue that in this case, it obfuscates simply because bitwise or'ing data is too low level and doesn't have a lot to do with rounding down without understanding the internal representation of the number.
I agree that code should be written at the highest level possible to express the intent, but sometimes that's not possible. In this case it's likely that performance wasn't an issue, and this particular idiom is actually of no value, but I contend that the principle remains: an idiom need not immediately be clear, and yet can still sometimes be the right thing to do/use.
Your last point is simply acknowledging that this is an idiom - what you say is characterising what an idiom, as opposed to an expression, is. It's not what you might expect, it's short, it accomplishes its purpose.
Finally, and again, I say that this particular example may not be a good one, but I say again that sometimes idioms are appropriate. You may disagree, but you haven't persuaded me. Our experiences no doubt differ, and may be the root cause.
It’s clear that using the bitwise or operator adds some bit of mental overhead, at least the first few times the reader looks at it, while floor is completely obvious and clear to the average novice programmer. This mental overhead slows the reader down, and makes the code harder to understand. If it’s just one line like that every few hundred lines, or some optimization in an inner loop that is clearly commented, that’s not a problem. But code written by someone who has decided that all of their tricky lines that first look like one thing but actually do something unexpected are actually wonderful “idioms”, is code that can become a serious pain in the ass for whatever poor sap has to build on top of it or fix its bugs.