5 ms·
This is only marginally better, as the computation logic is still "hidden" in the lambda. This is a very weird statement. It's hidden in exactly the place wh
by dkarl 8y ago
This is only marginally better, as the computation logic is still "hidden" in the lambda.
This is a very weird statement. It's hidden in exactly the place where it's needed! Where is it hidden from? Where else would you need it? If you're reading the code, it's right there, you don't have to click to definition like you do with Pair::next.
What should I get from reading Pair::next? There's no natural "next" for pairs of numbers. How would I guess that this guy's idea of "next" involves incrementing the first number and then squaring it? To make this equally readable you'd have to rename "Pair" or "next" so that the names were just as informative and easy to read as the lambda itself. So you end up with an unwieldy-but-meaningful name like SquareNumbersIterationState or SquaresSequence.State, which is the classic hallmark of OO programming run amok (aka, the Kingdom of Nouns.)
It's so much nicer to have the lambda inline and see exactly what's happening. One of my favorite things about functional style is that it doesn't force you to noun and name things that are easy to describe but hard to name. Sometimes something can just stand for itself instead of needing an awkward name. I mean, Java isn't the best at this, but this isn't hard to read at all:
pair -> new Pair(pair.index + 1, Math.pow(pair.index + 1, 2)))
- blankaccount 8y agoI really like your summary of the situation. I'd add that the author seems to have tried to fix a functional readability problem with OO techniques, ending up with the same kingdom of nouns problem - the worst of both worlds. I think the functional solution would be to separate increment and transform: Stream.iterate(0, i -> i + 1).map(i -> new Pair(i, Math.Pow(i, 2));
- kochthesecond 8y agoI also believe you can apply the "do one thing per line" here. it's a bit weird to do math directly in the constructor argument.