I wouldn't read too much into the performance difference of the tests. whichever one runs first is probably including the runtime startup and jit time in the results. I can't quite believe you'd be able to observe a difference in this short of an execution (you're probably not hitting a GC in either example).
As for the readability of you example, i think it's a great case. A case like you have here is where streams are the closest to making sense. The case is also pretty simple, making the syntax and mental overhead of the nesting scopes minimal. Streams don't get really problematic until you have something like:
var y = getRelated();
foo.stream().map(x -> {
if(y.processFoo(x))
return x;
return null;
});
Which some of the code i maintain looks like. I still have people telling me that "streams are more readable", even when we have shit like this in the codebase.
Even then, I still firmly believe the imperative example shown here is just better, because it's much more clear in its description of what the computer will do.
As for the negative reception, there's probably some amount of people who just don't agree, and it sucks that i get downvoted for that. There's probably also some amount who take offense with the argumentative (and sometimes hostile) way I've deliberately written some of the responses. I chose this style of argument because it makes it clear that i don't think people who like streams prefer a different "style" to me, i think they are wrong. People who like FP prefer a different style to me, people who like streams are just wrong.
6
u/ylyn May 25 '19
In some (many) situations, functional/declarative style is actually clearer and easier to reason about.
At least to me. YMMV.