3 ms·
Yes, specifically you should order arguments so the order will be on average from short to long. I try to write my code this way. map, filter, and reduce are t
by canjobear 11y ago
Yes, specifically you should order arguments so the order will be on average from short to long.
I try to write my code this way. map, filter, and reduce are terrible from this perspective. Unless you have do blocks like in Ruby or Julia!
Also, dplyr's %>% pipe operator is a great way to reduce dependency length in R code.
- jonahx 11y agoThanks for all your answers here! Could you elaborate on the above? Why does it imply short to long orderings are better? Also, I don't follow your comment about map/filter/reduce being bad except unless you have ruby-esque do blocks. Are you referring to something like map(<big function>, array)?
- canjobear 11y agoYeah, map(<big function>, array) creates a dependency that exists from when you read "map" to when you read the name of the array, potentially spanning a very long function. But if you have map(array) do <big function>, then you only have a dependency from "map" to the beginning of the function. In general, if you have a function call f(a, ..., y, z), when you parse that (mentally, or in a shift-reduce parser) you have to keep the function name f in memory all the way to z. So you want to make a, ..., y as short as possible. Similarly, dependency length minimization predicts that in English people will want to order expressions from short to long after a verb or preposition. There is a lot of evidence for this preference; it's been documented since the 1930s. If there were a programming language where the function name came after the arguments, like (a, b)f, then the best order would be long-to-short. Similarly, the DLM prediction for verb-final languages like Japanese is that people will prefer long-to-short orders. It appears that this preference does exist, but it is much weaker than the short-to-long preference among speakers of English-like languages.