How to apply some functional programming ideas in regular JavaScript.
1. Immutability
2. Simple functions
3. Not that simple functions
4. Combining OOP and FP
5. Functors
6. Monads
23. HOW TO REDUCE TEMPTATION
• Use const or let instead of var
• filter/map/reduce instead of for
• Object.freeze - to prevent object mutation
24. IMMUTABILITY: PROS
• Simpler code
• Less bugs
• Atomic object construction (object is either fully initialised, or doesn’t exist)
• Less temporal coupling (when order of initialisation matters)
• Simpler caching
• Thread safety*
25. IMMUTABILITY: CONS
• Higher CPU impact (more things to do)
• Higher memory impact (and more GC)
• Higher brain impact (OOP often comes with state)
49. NOT THAT SIMPLE FUNCTIONS: PROS
• Flexibility in using and reusing functions
• Functions tend to be smaller
• Memoization (“Simple functions” PROS list was already too
long)
50. NOT THAT SIMPLE FUNCTIONS: CONS
• Sometimes code looks cryptic
• Might introduce negative performance impact
51. COMBINING OOP AND FP
Shell - OOP, IO, side-effects
Core - FP, logic, pure functions
Core
Shell
57. SHELL/CORE: PROS
• Main bugs will come from the shell, which is small
• Core is easy to test. Unit tests should be enough
• Shell is the one that needs integration tests (shell is small!)
• Command query responsibility segregation out of the box