whatsoever things are pure... think on these things.
- Philippians 4:8
( 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
( 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.
}
Side Effects
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"
let aFlag = false;
const checkForState = (thingToCheck, thingToLookFor) => {
if (thingToLookFor in thingToCheck) {
aFlag = true;
}
}
const hasThing = (thingToCheck, thingToLookFor) =>
(thingToLookFor in thingToCheck);
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];
const randomBetween = (min, max) =>
Math.floor(Math.random() * max) + min;
pure functions
impure functions
Anytime your application must accept something from outside itself.
pure functions
e.g. service call
e.g. render
services
data base
App