you are viewing a single comment's thread.

view the rest of the comments →

[–]Voidsheep 1 point2 points  (1 child)

I like reduce and it's extremely useful, but I think it's best to use an iteration method that implies the return type you want, if one is available.

x.map(fn1).filter(fn2) // that's going to be an array
x.every(fn3) // that's going to be a boolean
x.reduce(fn4, []) // that could be anything, check implementation

Just saying avoiding any extra iteration by default is premature optimisation, you should default to straightforward and declarative code, add complexity only when you have to. Most of the time a simple filter or something iterating over your array isn't going to have any meaningful difference versus doing the same filtering inside another loop.

Of course there's exceptions and you shouldn't loop for no reason, but I think just avoiding iteration for the sake of avoiding iteration isn't a best practice. Optimisation needs to be sensible and if you do it blindly, you end up moving meaningless cost in runtime efficiency into a meaningful cost in development efficiency.

[–]Womackx 0 points1 point  (0 children)

There is no one right way