DEV Community

loading...

Discussion on: Here's Why Mapping a Constructed Array in JavaScript Doesn't Work

Collapse
sreisner profile image
Shawn Reisner Author

The for loop is an appealing construct. It's simple. You understand exactly what's happening algorithmically without much of a second thought. The appeal of the higher order map function is that it abstracts away the algorithmic details so that you're telling the code what to, not how to do it. In my experience, code written this way is easier to understand, debug, and maintain, and generally produces fewer bugs. This becomes more true when we need to chain the effects of these higher order functions, but even in cases like this we still benefit from the fact that there's almost nowhere for a bug to hide.

Collapse
belinde profile image
Franco Traversaro

I don't agree... You know, you've just written an article about this approach not working with a naive approach 😁

That was obviously a joke, but still for me it's very hard to understand what that code do. And I feel a bit of horror thinking about the CPU cycles wasted with so many calls...

Thread Thread
sreisner profile image
Shawn Reisner Author

Not quite. I added the bit about the for loop being perfectly fine at the beginning of the article on purpose - because it is! I'm glad you found it thought provoking. I appreciate the feedback! ❤️