I associated with the word "pure" a lot and long before I ever heard about pure functions. I love to see purity in almost anything and nature's the best example.
Pure laughter is another thing I admire. I recall saying "I love the purity of" about lots of things. In my mind, pure means one thing and one moment only. It has no history and it does not affect the future.
So did my definition of the word "pure" change when I learned about Pure functions?
The answer is a big NO! In fact it added more meaning to it.
Before jumping directly to pure functions let’s revise the definition of functions. When we repeat ourselves while programming, we make a function out of the repeats. Sometimes we make them just to make the code more readable. This could be a part of "separation of concerns".
A function takes inputs and returns output. From the previous line, we know that after executing the function, our program cannot be the same again and lol we cannot deexecute it. Is "deexecute" even a word?
Be we write code in such a way that anything can change, we lose track of what our code is doing.
Such functionality is called mutation in programming, we keep mutating variables which can affect the output of our program. No doubt programs are written to perform certain tasks for humans and that requires change. For example updating the balance every month and displaying the total current amount which will include your monthly salary.
Mutations are required and can be managed with a stateful logic system At the same time lots of parts of our code don’t require mutating state but do perform certain tasks for our program, such tasks are performed by pure functions.
So what is a Pure function?
A pure function performs a task only with the parameters that are passed to it and returns the output. Their task doesn't involve mutating variables outside its scope. Hence passing the same parameter will always result in the same output.
A more layman example would be; A conversation between two individuals is pure if their words don't require more analyzing and it’s just related to the current environment, time and moment. Woww that conversation would be so stress-free.
Implementations of pure function output similarly. Let us dig into what stress-free means in programming
We especially get stress when some part of our program is not behaving as expected, writing Pure functions helps to mitigate that stress as our function becomes easily testable. Or it can be another way around! Write the test first, and the pure function definition helps you write and think about one task and its obvious expected output. Especially if you are using typescript then you can even mention what types of arguments and output a function is expected to take and return. It helps to make our code more reliable and with reliability comes stress-free development. This style of writing code is called Test Driven Development. Next thing you know exactly where to put a debug() in your code :P
Javascript already supports inbuilt pure functions on an array like map, concat, reduce, filter, and many more, let’s check out with an example of the reduce function,
In the above example, I love the way reduce function which itself is PF uses a pure function(getSmallestNumber) and an accumulator to flatten the array to get the single smallest number. When the compiler identifies the pure function the above code can be executed in a different thread and the reduce function is run on the results of each thread, to give the smallest number. Definitely performance increases on a multi-threaded program than simply running it in a single thread. It is called Parallelization.
The last thing(for this article) which caught my attention while understanding pure functions, is their dependency array. When a function with the same input returns differently because it depends on some state of the program. Hence while writing such functions we have to mention the dependency array.
I would like to continue extending the explanation of the last part in the next article. Hope you have fun reading it. Please give me helpful feedback, I would really like to improve myself. Thanks!!
Top comments (0)