DEV Community

Cover image for Back to Basics - Pure Functions
Molly Nemerever
Molly Nemerever

Posted on • Edited on

Back to Basics - Pure Functions

Pure functions are one of several core concepts of fundamental programming. I'm sure you've written them, but were you able to identify that you were writing a pure function? Did you realize the importance or key traits of a pure function? If you're not sure then you've come to the right place. Let's review the basics of pure functions together!

chris pratt gif looking excited
so excited

What are Pure Functions?

Simply put, pure functions are functions which accept argument(s), compute a value based on the argument(s), and return a value. They always do this. Sounds really basic - right? Well, pure functions kind of are! But let's dig a little deeper into some additional key characteristics.

Pure functions must always accept at least 1 argument. This argument will remain unaltered but will be used in the computation which takes place inside the body of the function. Additionally, there must always be a return value. This is perhaps a dead giveaway of a pure function - the return value will always be the same when called with the same arguments.

Take the code below - run plusFive 1 million times and you will always get the same return value when passed the same argument.

basic pure function example
very basic example, I'm sure you've used something like this before!

It's also important to note what pure functions don't do. They shall not cause side effects such as altering the arguments that were passed in, changing your application's state, setting/updating global variables, or any other side effect like triggering a fetch request. They're simple and only depend on their arguments to compute and return a value.

Pure Functions in Your Program

Cool - now we've got the definition down let's take a look at how they might be used and some advantages.

Because pure functions are independent of your program's ecosystem, that makes them easier to debug and refactor. You can expect the same output given the same input - so expect to write simple tests for these functions and if problems do arise you'll be able to quickly diagnose the issue. You can also most likely dismiss them as a culprit of other bugs knowing that these pure functions don't cause side effects. Another plus of no side effects is that when you refactor your pure functions you can kiss away any anxiety you might have about accidentally tweaking another corner of your program. Refactoring these functions won't change others in your program since they were never connected to begin with.

Examples of Pure and Impure Functions

First up - let's look at an impure function. Notice how the moveUpGrade function alters the argument that was passed in? That means the function is creating a side effect therefore it is impure.

impure function example
impure function

Let's refactor the code to make the function pure. Below you can see we use the spread operator to create a new student object. Instead of altering the exact object that was passed in and causing a side effect, we make the update desired and return a new object. Pure!

pure function example
pure function

And that's it for Back to Basics - Pure Functions! They're quite straight forward, can help maintain clean code, encourage simple refactoring, as well as quick testing/debugging.

that's all folks gif

Top comments (0)