3 ms·
> People don’t speak entirely in idioms unless they are totally off their rockers. My first thought on reading that was to think of Darmok and Jalad at Tanagra
by tzs 9y ago
> People don’t speak entirely in idioms unless they are totally off their rockers.
My first thought on reading that was to think of Darmok and Jalad at Tanagra. Then I realized that was allegory, not idiom. Then I realized I had no idea what the actual definition of "idiom" is, and looked it up: "a group of words established by usage as having a meaning not deducible from those of the individual words (e.g., rain cats and dogs, see the light)."
...and now I'm confused because nearly everything we call an idiom in programming isn't. What would be the correct term for whatever it is we are currently calling programming idioms?
For example, one from the idiomatic Python book that the article criticizes:
def contains_zero(iterable):
# 0 is "Falsy," so this works
return not all(iterable)
That one was confusing to me, because I'm not a heavy Python user. I've never encountered all() before. However, after looking up the all() function in the Python documentation that code becomes immediately obvious.
It seems that most of what we call idioms in programming are just using more advanced or more obscure language features or library functions to do something in a more compact or efficient way at the cost of immediate clarity to people who are not as advanced in the language. We've decided what these do is needed often enough that we agree that these are the way people should do these things, even if they don't understand what is actually going on.
I'm not sure programming languages can have actual idioms, except perhaps in the case of language bugs or bad/missing documentation.
The combination of language plus hardware could have idioms, like nested loops iterating a multidimensional array should have the outer loop on the first dimension, the second loop on the second dimension, and so on to minimize jumping around in memory and so hopefully interact better with the cache.
- mercer 9y ago> That one was confusing to me, because I'm not a heavy Python user. I've never encountered all() before. However, after looking up the all() function in the Python documentation that code becomes immediately obvious. > "a group of words established by usage as having a meaning not deducible from those of the individual words (e.g., rain cats and dogs, see the light)." Perhaps this is actually a really good illustration of 'idiom' meaning the same thing (roughly) in programming as it does in day-to-day use. I'm not a Python dev, but I'm willing to bet that a Python dev knows the all() function, in the same way that a Javascript dev knows about something like .bind(this), or querySelectorAll, or a CSS 'dev', the horror, "margin: 0 auto;". If that's not the case I might be wrong.
- empthought 9y agoIt's even worse; the book is wrong. That implementation of contains_zero is not idiomatic to anyone who writes Python.
- hxtk 9y agoNot only is it not idiomatic; it's functionally incorrect. An iterable containing `[]`, `None`, or any other value that is false in a boolean context would return true. They've assumed that `(0 -> false) -> (false -> 0)`, which is simply not true. Even if you're dealing with a context that cannot pass in an iterable with non-zero "falsy" values then it needs to be explicitly documented. At that point, why not "document" it by being explicit with the code. This is how I'd do it, but I feel like it could be made cleaner: def contains_zero(iterable): return any([i is 0 for i in iterable]) Edit: I forgot how to python. It's `return 0 in iterable`.
- deleted 9y ago[deleted]