This post is from a low-reputation account and contains an unverified outbound link. Be cautious before clicking external links.

Pure Functions

Words
174
Reading
1 min
Listen
Play
8y

What are pure function?
Pure functions are functions that return the same output every time given a certain input. They also do not produce any side effects such as network requests or data mutation.

Screen Shot 2018-04-30 at 1.20.12 PM.png

Impure Functions
An impure function is one that depends on some state in execution environment. Here is an example:
Screen Shot 2018-04-30 at 1.30.43 PM.png

What's the point?
Pure functions are easier to test and easy to reuse. They are also easy to chain. With pure functions you can create complex data processing through the composition of simpler pure functions.

For example, lets say you want to test a method inside a controller. If that method uses state (i.e. a user session or POSt input) to create its output then you already have to know more about that method's execution environment in order to test it effectively. On the other hand, if it is a pure function, the environment in which it runs doesn't matter.

For further reading check out: https://medium.com/@jamesjefferyuk/javascript-what-are-pure-functions-4d4d5392d49c

I hope you enjoyed this installment of Zack knows some stuff. I'm a developer, founder, lecturer
https://facebook.com/ransomly
https://instagram.com/positivelyZack
https://instagram.com/ransomly_inc

Pure Functions | Ecency