whatsoever things are pure... think on these things.

- Philippians 4:8

Functional Purity

( revealing your app's hidden complexity )

Functional purity is a principle of functional programing and is defined as...

 

...given the same input, [a function] will always return the same output and does not have any observable side effect.

https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch3.html

A side effect is defined as...
 

...a change of system state or observable interaction with the outside world that occurs during the calculation of a result.

https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch3.html

A reliance on external state is one of the most common sources of unintentional side effects.

This is your function

This is your function on state.

Fratelli Functions

( it works don't it? )

It may work perfectly, but it will only work for your app, and only at this particular iteration.

Internal state binds your code to your application.

Meaning one has to understand the application as a whole to understand one discrete piece of code.

Left unchecked or uncontrolled, state infects your application making it less resilient, less reliable, and less maintainable.

Fun & Useful fact: Impure functions increase cognitive load on developers and de-optimize compilers.

In contrast, pure functions are completely portable.

Unbinding them from the context of your application.

One need only understand the function to contributing meaningfully to it's maintenance or performance.

It's portability makes it dramatically easier to test as well.

Pure functions inoculate that part of your application from the infection of unregulated state.

Fun & Useful fact: Pure functions break less than impure functions.

Qualities of Pure Functions

  • Cacheable
  • Portable
  • Serializable
  • Self-documenting
  • Testable
  • Reasonable
  • Parallelizable

 

Flavors of Impurity

  • Implicit dependencies
  • External assignments
  • Mutated arguments
  • Referential opacity

}

Side Effects

Implicit dependencies

let greeting = "Hello";

const sayHello = (name = "What's yer face") => 
    `${greeting} ${name}`;
const greet = (greeting="Hello") => 
    (name = "What's yer face") => 
        `${greeting} ${name}`;

const sayHi = greet('Hi');
const sayHiTo = sayHi('cuz'); // "Hi cuz"

External Assignments

let aFlag = false;

const checkForState = (thingToCheck, thingToLookFor) => {

    if (thingToLookFor in thingToCheck) {

        aFlag = true;

    }

}
const hasThing = (thingToCheck, thingToLookFor) => 
    (thingToLookFor in thingToCheck);

Mutated Arguments

const prop = (someObject, propName, newVal) => {
    if (newVal) {

        someObject[propName] = newVal;

    } else {

        return someObject[propName];
    }
};
const setProp = (someObject, propName, newVal) => 
    ({...someObject , [propName]: newVal });

const prop = (someObject, propName) =>
    someObject[propName];

Referential Opacity

const randomBetween = (min, max) => 
    Math.floor(Math.random() * max) + min;  
  

You can't always avoid impure functions, but you can control them.

Push impurity to the edges.

pure functions

impure functions

Main sources of unavoidable impurity.

  • I/O
  • User actions
  • Network requests
  • File system changes
  • Rendering
  • DOM Selection

Anytime your application must accept something from outside itself.

Push impurity to the edges.

pure functions

e.g. service call

e.g. render

services

data base

App