Fundamentals of Functional Programming in JavaScript - p-patel/software-engineer-knowledge-base GitHub Wiki

https://app.pluralsight.com/library/courses/javascript-functional-programming-fundamentals/

What is Functional Programming

What is Functional Programming?

  • Sibling of OOP
  • Functional programming part of larger paradigm of declarative paradigm, in contrast to the imperative paradigm
  • Mathematical functions (pure functions). Don't depend on data other than what's passed in and don't alter any data than what's returned
  • Functional programming values immutable data which is easier to reason about

Why Use Functional Programming?

  • Easier to reason
  • In imperative programming you need to reason about two dimensions - 'what is it doing?' and 'how does it do it?'. In declarative programming you reduce this to just one - what is it doing?
  • Testing is easier as unlike in an imperative paradigm, setting up the application state is easier (it's just the values passed into the function) and asserting correct behaviour is easier (it's just checking the value returned by the function)

Declare What You Mean

Libraries

  • RamdaJS, lodash/fp

The Power of Functions

First Class Functions

  • pass functions as arguments, assign functions to variables

Returning Functions

  • a function that can return other functions
  • currying converts a function that accepts multiple parameters into a series of functions that each only take 1 parameter e.g. byId(2)(item)
  • partial application - supplying fewer parameters than needed, with the remaining parameters being provided to the returned function later
  • note the difference between currying and partial application. With both concepts you can build up functions that don't yet have all the parameters they need
  • see Ramda's R.curry() that will work with any number of parameters

Pure Functions

    1. doesn't depend on any data other than what's passed in
    1. doesn't modify any data other than what's returned

Function Composition

  • combining functions to create a new function
  • use R.pipe() and R.compose() to improve readability of function compositions

Side Effects May Be Harmful

Side Effects May Be Harmful

  • 3 parts of functional programming - declarative (what vs. how), power of first-class and pure functions, data doesn't change

What Are Side Effects?

  • Alters data outside of it's scope
  • A program must have side effects; need to be deliberate about when and why they occur
  • Side effects can slow debugging

Why immutability is important?

  • Immutable structures make programs easier to reason about
  • With immutability, each function has it's own copy of the data that cannot be altered unexpectedly by another function

Immutability and Performance

  • Immutability is still performant as copying data copies the reference of object properties, not the entire object for object properties that are unchanged when the new copy of the object is being created. Also garbage collection ensures old objects that no longer required are collected and the memory recycled

Seamless Immutable

  • No native JavaScript immutable structures, note const myObject = {}; indicates the object is read-only but properties on the object can still be added and deleted
  • 'seamless-immutable' lib enables immutable data structures and works with plain old JavaScript data structures
  • e.g.
let myImmutable = Immutable([3,5,1,2,4]);
myImmutable.sort(); //sort cannot be called on immutable structure
  • instead this can be sorted using R.sort() which does not alter myImmutable
  • use .asMutable() to create a new mutable copy of the data structure when required
  • Immutable() can be used for arrays and objects
  • .set() can be used to set properties on a newly created immutable object
  • .setIn() can be used to provide a path to a property to set a value on

Summary

  • seamless immutable can be introduced into a codebase by limiting it to an area you are working on and exposing mutable data structures outside of that using asMutable() to avoid forcing other developers to embrace it as well. Adoption can be controlled and managed over time in this way.

Testing Your Functions

Common Test Problems

  • Setup of tests is time-consuming - requiring set up of dependencies required to perform the test

Testing With Functional Programming

  • Testing pure functions is simplified as it only requires checking/asserting the value returned by the pure function

Summary

  • Simple setup - no mock/stub any other functions
  • Declarative nature of functional programming - in particular simplification of loops
  • Power of functions - as variables, compose functions of other functions
  • Side effects when state is altered outside of the scope of the function
  • Performance - with object reuse and garbage collection
  • React, Redux and Ramda all use functional programming