So I understand the logical reasoning behind not doing mutations with a functional.method with an emphasis on immutability but in this case I don't see much if a problem would you care to elaborate what's wrong with it?
The intent is no longer clear. .map has a very clear intent - give me a new array with some transform performed to all the elements. Don't mutate the original array.
It communicates conflicting intentions. The map says "any side effects are a mistake and should be put in the bug tracker" while the loop body says "I'm nothing but a side effect." That kind of thing makes the code harder to read for the next person two years down the line.
No particular reason other than the fact that I can be a little cynical and part of me believes the community will opt for map simply because it's shorter and "cooler" to use functional programming techniques. The fact that there is nothing in Javascript (that I know of anyway) to enforce referential transparency means that use of the FP techniques will likely be little more than lip service to the concepts.
I'm pretty hard on the Scala and Haskell communities when it comes to being able to make FP practical and accessible for everyday use, but at least they understand how to get the benefits of FP. I currently do not have any such confidence in the Js community.
I'm not sure, I might quite possibly have misinterpreted (apologies if that's the case), but just in case, the motivation behind not using map and reduces to perform mutations isn't out of an idealistic "functional programming is good" or "mutations are bad" dogma. It's for the benefit of making your code easier to understand for yourself and others
Specifically, it's an implicit promise that the code inside the function, no matter how complex, can be safely ignored as long as the value it returns is as expected. There is huge value in being able to rely on the (albeit implicit) referential transparency for maps and reduces in the amount of code you can skip when you're combing for bugs or trying to understand a section of a program someone else wrote.
And I can tell you of one way of enforcing referential transparency - code review :)
I've run into a few javascript programmers who've made the same mistake with misusing map (2 juniors and one self-taught rockstar), but after I've explained it the responses were always immediately "that makes sense". That gives me hope that while it may be an easy mistake to make, it won't get touted as a best practice because people will understand why they shouldn't be doing it as soon as you explain it to them, and hopefully pass it forward :)
6
u/enkideridu Jun 10 '16
Please don't use
maps to perform mutations