In this tutorial, you will learn about an important functional programming principle that you can use when writing Javascript, pure functions. In this tutorial, you will learn about pure functions and why you should be writing your code to adhere to this principle. You do not need to learn any new syntax to start writing pure functions. Writing a pure function is a programming style. A function is classed as pure if it meets these two statements:

  • Given the same arguments, the function always returns the same result

  • A pure function does not change the state of any of its arguments

  • A pure function does not depend on any external dependencies

This sounds simple enough, but why bother? If you strive to make as many functions as possible pure, you guarantee that your code:

  • Does not have unexpected side-effects.

  • Will not break anything else outside the function.

  • Guarantees the order of execution

  • Unit tests are easier to write

  • Caching becomes easier. If the same inputs produce the same outputs, you can easily cache the results of the function.

  • Easier for developers to understand, as all code is self-contained

Should All My Functions Be Pure?

The answer to this is simple, no. It's impossible to write a whole app with pure functions only. With some conscious effort though you will find that when you consciously try to make each function you write pure, you will end up writing more robust code naturally.

For a simple example, say you have a function that has an array as an argument. If you apply one of the out-of-the-box JS functions like splice(), or, reverse() then you can accidentally make a function impure. Both of these Javascript functions change the underlining array. Not all Javascript functions mutate the state. Simply thinking in terms of pureness will lead you to write more robust code, which is great!

When you mutate arguments, your function is called 'impure'. Impure functions make life harder. Impure functions are harder to unit test so consequently, when functions are impure it will increase the chance that a bug may slip through your testing process and make it into production. In the example above, to keep the function pure, you can clone the array that is passed in, performed the mutation and returned the clone. The original array will remain unchanged and the function is pure.

What Are Side-Effects?

If a function is impure, it is known to cause a side-effect. Side-effects can include these changes:

  • Modifies a mutable data structure

  • Modifies an argument

  • Calls an API

  • Reads from disk, or, a database

  • Throws an exception

In your code, sometimes doing these types of operations is inevitable. As I mentioned above, it's impossible to have an app with only pure functions. Side effects should be avoided where possible because they make the behaviour of your functions unpredictable. When a function has no side effects, it can run anytime you want with no qualms. Given the same input, it will return the same output.

Pure Functions Summary

As that simple example demonstrates, when you code, with a bit of conscious effort you can keep your functions pure with some slight refactoring. On a day-to-day basis, if you keep this principle in mind you'll end up writing more robust code that will ship with fewer bugs, happy days! Happy Coding 🤘