Pure Functions
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.
Impure Functions
An impure function is one that depends on some state in execution environment. Here is an example:
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
Leave Pure Functions to:
Read more #purefunction posts
Best Posts From positivelyzack
We have not curated any of positivelyzack's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From positivelyzack
- Adding props or functionality to React children
- Positive Product Design
- Half Life: The Decay of Utility of Software Knowledge or why you should learn to learn
- Pure Functions
- Deploying a fullstack React app to Heroku
- Components with Props In React
- The structure of app created with the create-react-app tool.
- ES6 Javascript exports, imports and the difference between 'default export' & 'export'