5 ms·
I think that code readability is different from system comprehensibility, and that readability is merely a subfactor to comprehensibility. Readability often mea
by threatofrain 11y ago
I think that code readability is different from system comprehensibility, and that readability is merely a subfactor to comprehensibility. Readability often means the time it takes for me to grok a function, module, or unit of code, and often what fits on my eyeball.
System comprehensibility is how long it takes for me to learn the minimal set of atoms in order for me to comprehend an entire system.
Rob Pike criticizes map and reduce and other functional idioms as not machine simple. And although Rob Pike doesn't say this, I might argue on his behalf that functional code has a mild reputation for less readability.
But I would argue that functional idioms permit tasteful tradeoffs between machine simplicity and system comprehensibility. I would also argue that when a system gets to a certain largeness, I would be sometimes okay with sacrificing some readability and machine simplicity for system comprehensibility. I would also say that although I might lose some performance advantages with a functional approach, the improvement to system comprehension might mean an improved cognitive capacity for the human to optimize for distributed multi-core contexts, which might offset the losses from machine simplicity.
- pcwalton 11y ago> I would also say that although I might lose some performance advantages with a functional approach, the improvement to system comprehension means better human optimization of distributed multi-core contexts. You don't even need to bring in parallelism for functional approaches to gain in performance. Here's an example (in Rust) of how you'd implement collecting an iterator into a vector using the "machine simple" approach: let mut array = vec![]; for element in some_iterator { array.push(element); } Now for the functional, less "machine simple" approach: let array = some_iterator.collect(); Which is faster? Without knowing the details of the compiler, you might think the former is faster, because there are fewer function calls and less indirection. But this is wrong, as the compiler will trivially inline "collect" for you and eliminate any indirection. In fact, it turns out that the second approach is often significantly faster, because the standard library contains the equivalent of: let mut array = Vec::with_capacity(some_iterator.size_hint()); for element in some_iterator { array.push(element); } That is, it uses the size_hint() method to avoid reallocating during the collection operation, reducing the number of memory allocation and array copy operations to O(1) instead of O(log n). What are the chances that you'd do this important optimization manually? Not high: it adds noise, it's esoteric, and it's easy to forget. But if you use the functional idiom, you benefit from the optimized approach every time. That is the true benefit of abstraction, which is really the key point here. "Machine simple" and "fast" are frequently in opposition, and too often people think that the former implies the latter.
- lomnakkus 11y agoAn excellent example. This is also one of those things that is hard to profile away, because it essentially leads to a very flat profile because you're probably forgetting to use with_capacity()/reserve() all over the place not just in single very-costly cases[1]. So you end up wasting, say, 1% CPU time all over the place... add up enough of these time-wasters and soon you're going to be wasting significant amounts of CPU time for no good reason. Obviously, this kind of thing isn't a magic bullet, but in general I'd say the key for any kind of program is to express your intent at the highest level possible. This benefits both optimization and human readers! (That obviously means that the programming language must support such abstraction in a non-leaky way.) [1] Which would show up prominently on a profile.
- tbrownaw 11y agoNot really... the wasted time is still all malloc() / realloc() / whatever under array.push(), and a half-decent callgraph profiler will catch that. array.push() will score far higher than it ought to, and the bulk of its time will be the reallocation. Source: I've used oprofile, and later perf, to hunt exactly this kind of performance bug. . Of course, fixing a pervasive issue like that is much more tedious than just finding it. :-(
- justicezyx 11y agoArray copying is on amortized...
- serge2k 11y ago> tradeoffs between machine simplicity and system comprehensibility I honestly have no idea what this even means.