positivelyzack avatar

Pure Functions

positivelyzack

Published: 30 Apr 2018 › Updated: 30 Apr 2018Pure Functions

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.

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

Leave Pure Functions to:

Written by

Founder and Full-stack Developer Instructor

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