r/functionalprogramming • u/kinow mod • Jan 02 '23
FP Functional Programming - How and Why
https://onsclom.bearblog.dev/functional-programming-how-and-why/
11
Upvotes
r/functionalprogramming • u/kinow mod • Jan 02 '23
View all comments
Show parent comment
7
u/josephjnk Jan 02 '23
In a multiparadigm language like JS/TS I generally think that the enemy is shared mutable state, not local mutable state. It’s often faster and clearer to create and modify an object inside of a function and then treat it as immutable after it’s returned to a caller. Shared mutable state is still dangerous though, and I’ve seen multiple production issues caused by people writing functions which mutated their arguments. These bugs were generally hard to reproduce and locate.
Also, if you’re going to lean into immutability, I generally recommend using a library like Immutable.js. The performance of libraries that use structural sharing are often better than you’d expect, and a good high-level API for persistent data structures makes a big difference compared to manual slicing and cloning. (Plus, IMO, value semantics for equality and collections are both a very good reason to use a library.)