3 ms·
I know most will disagree but I find code the above hard to read. I'd much prefer const people = list.stream(); const peopleOlderThan50 = people.filter
by greggman2 7y ago
I know most will disagree but I find code the above hard to read. I'd much prefer
const people = list.stream();
const peopleOlderThan50 = people.filter(??);
const average = peopleOlderThan50.mapToInt().sum() / peopleOlderThan50.length;
Or some such thing. The more complex the chain the harder it is for me to read. Does anyone else have that experience?
- joostdevries 7y agoI agree. Naming parts of your code is important. An alternative to named local vals is to either use named functions instead of lambdas: people.filter(olderThan50) or (I use Kotlin) use named extension functions: people.countOlderThan50()
- splittingTimes 7y agoIn any nontrivial application, doing this for many of your types, does this not bloat your API considerably, which adds to the cognitive load?
- sooheon 7y agoThere's a spectrum here. Someone even more explicit than you may insist that `average` should be split up into explicit bindings for the mapping to Int, the sum, and the count. Also, for non-toy examples, there are real benefits to composing transformations, so that you don't pay the memory and performance costs of assigning intermediate sequences.
- ThrowawayR2 7y agoYou're not alone; those long chains are annoying to decipher. The other benefit of what you wrote is that if you have to track down a bug in the chain, you can actually examine the intermediate variables directly instead of having to pick apart a long chain so that a variable is exposed for you to look at in the debugger.