9 ms·
Write code to be as clear and expressive as possible, then optimize for performance when you know performance is a problem. This is why I don't mind the perform
by RandomBK 11y ago
Write code to be as clear and expressive as possible, then optimize for performance when you know performance is a problem. This is why I don't mind the performance cost of new language features like this. 90% of the time it won't matter, and you can optimize the other 10.
- haddr 11y agoSometimes it is the loop that brings most overhead to the processing time (imagine big array of ints, and some simple transformations). In such case refactoring would be easy, but anyway...
- userbinator 11y agoThat's the mindset that eventually makes everything slow and you can't easily see that 10% anymore because it gets more spread out the more abstractions you introduce. When loops stop looking like loops, it makes it rather harder to find them.
- vitalyd 11y agoAka "flat profile". Using these things in full force requires really banking on JIT doing the right thing (if perf is a concern), which is optimistic. A simple loop is just as readable as these one liners, and carries less risk of not being compiled as tightly.
- sacado2 11y agoThese constructs don't always make code any easier to understand / maintain. I have played a lot with NetBeans' feature that automatically translate loops with their "functional" equivalent. Sometimes the code was so clever I couldn't understand what was happening. Sure, code is more compact, but compactness is not an end in itself.
- justinhj 11y agoCompactness is more of a side effect and can be a detrimental one. Personally I find that this kind of code is more declarative of intent than the more imperative for loop. By using particular functional tools for the job you're avoiding any possibility of a bug in your looping construct and making your purpose explicit. I also like how it removes boiler plate to handle different container types making it easier to switch to different ones.
- Glide 11y agoThis reminds me a lot of Resharper's refactoring common things to LINQ in Visual Studio. A lot of times I just ran it thinking "oh wow that's a neat way to do it" and then reverted it back because it wasn't a common idiom or it made it much harder to understand. The same thing is going to happen with Java 8 until the feature becomes less shiny.