4 ms·
My view is that this shows again that worse is better. The old form is better because it is awkward and it can only be improved by turning it into a function.
by pierrebai 9y ago
My view is that this shows again that worse is better. The old form is better because it is awkward and it can only be improved by turning it into a function.
Within a function, each switch branch can be a return. Then you get the benefits:
* Make the original function shorter.
* Give a descriptive name to what you're computing.
* Make the computation reusable.
It's what I find lacking with lambdas: it makes it too easy to forego good refactoring because it makes too easy to write one-off code that in reality will be rewritten many times in different places.
- tigershark 9y agoNo, worse is worse. Pattern matching is much, much cleaner than a switch with the branches extracted in separate functions. I don’t think that anyone using languages that support proper pattern matching can disagree. If someone is writing the same lambda n times instead of extracting it in a common method the problem is not the lambda function, but the developer. Closures are probably the best feature in Java and they have been delayed enough.
- jjnoakes 9y ago> Pattern matching is much, much cleaner than a switch with the branches extracted in separate functions I don't think that's what is being suggested. I think the suggestion is to move the entire switch statement to a separate function, use 'return' in the switch cases, and have the caller assign the variable once, from the return value of the function.
- neandrake 9y agoRe: lambdas - if one-off code is being rewritten so many times maybe it's small enough that it doesn't warrant it's own function? I've never run into that issue and love to use lambdas where possible -- in fact the use of streams api has frequently been a source of simplifying existing boilerplate loop-type code. Any time lambda code seems to be repetitive and large enough to warrant refactoring that's usually what happens -> throw it into a method and call from the lambda (or just use method reference syntax if possible).