by RiderOfGiraffes · 16y ago · 0 comments
> ... if someone else sees your code
> they won't know what is going on.
The same is true of all idioms in a language. But when you see something you don't understand you work through it, see what's going on, acquire the idiom for yourself, and move on.I don't see that as a problem. If you're really worried about readability, wrap it in a macro.
Only giving the same answer as floor for positive numbers need not be a problem - you just need to know that it will do what you want in the context you have. So often one uses "floor" because it gives the right answer, and not because it's actually "floor".
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.