cover-img

Pure vs Impure Functions

Understand pure and impure functions with side effects

23 June, 2022

22

22

0

These are the two terms that you always hear in a programming language called Pure and Impure functions.

You know Pure Function is always dependent on arguments and there should not be any side effects.

What hack is Side Effects? ☠️

When you try to use external code in the function or if you are mutating a variable then it creates a Side Effect.

Example:


As you can see in the above snippet, mul the function has a dependency on an outer variable called num. Also, mutating num value, which makes its impure function.

Let’s checkout one more quick example:


So, what do you think about the above snippet? πŸ€”

.

.

.

Yeah! this is also an impure function πŸ˜΅β€πŸ’«

As you all know that JavaScript is synchronous, using console or any callback or promise/fetch will make the function asynchronous.

Here using the console, which is a Web API makes it an impure function.

Let's checkout other side effects with examples:

  1. DOM Manipulation, any callback or reading/writing files

    
    
  2. Updating or Modifying a global variable in pure function

    
    
  3. Here also, mutating outer variable which is depending on an external variable.

    
    

Let's Understand the Pure and Impure function, as now you have an idea of the side effects

Pure Function

  • It always returns theΒ same resultΒ if the same arguments are passed
  • It never depends on any state/data/change during the execution of a program
  • It always returns something
  • Here, writing test cases will be straightforward

Impure Function

  • Changing the internal state of any argument which has been passed
  • It may take effect without returning anything
  • Writing test cases will be a bit complicated as there may be side effects

Pure and Impure Methods

These are pure methods:

  • Array.map()
  • Array.reduce()
  • Array.filter()
  • Array.concat()
  • ... (spread syntax, which is mostly used to create copies)

These are impure methods:

  • Array.splice()
  • Array.sort()
  • Date.now()
  • Math.random()

Bonus Point πŸ”–

You can clone an external state into a pure function.

Cloning an external state into a pure function does not make the function impure.

Example:


In the above snippet, you are using the ... spread operator in the fullName function. It will create a clone of it and then update it. So, it doesn't depend on the name variable.

Thanks for reading the article!

Hope you found this article useful. If you have any questions, feel free to drop them into the comment section.

22

22

0

Suprabha
Senior Frontend Engineer πŸ‘©β€πŸ’» | Public Speaker πŸ—£ | UI/UX Designer 🎨

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

Β© Copyright 2024. Showcase Creators Inc. All rights reserved.