DEV Community

Cover image for Pure vs Impure Functions

Pure vs Impure Functions

Sandra Spanik on March 18, 2021

Software engineering is full of jargon. Occasionally, to grasp the true meaning of the seemingly simplest of words, one must waddle through many mu...
Collapse
 
eecolor profile image
EECOLOR

There are a few use cases where an implementation of a function could be considered 'not pure' while, from the outside, it is pure.

let complexValue = null
export function getComplexValue() {
  return complexValue || (complexValue = calculateComplexValue())
}

function calculateComplexValue() {
  // this is always the same
  ...
  return ...
}
Enter fullscreen mode Exit fullscreen mode
export function mapValues(o, f) {
   return Object.entries(o).reduce(
    (result, [k, v]) => (result[k] = f(v, k, o), result),
    {}
  )
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
toanleviet95 profile image
Toan Le Viet

Yah I agree that Impure Function is also super useful in certain contexts. Sometimes, we use in-place solution to reduce the space complexity instead of creating copies with Pure Function

Collapse
 
rbauhn profile image
Robin Bauhn

Nice article!
I think that it is worth pointing out could be that spread syntax only is pure for primary types. For objects and that sort of stuff it is only the reference that's being copied so any mutations would also mutate the orignal object.

Collapse
 
brad_beggs profile image
Brad Beggs

.sort() is also impure, sorting the input in-place.

Collapse
 
sanspanic profile image
Sandra Spanik

Thanks for the suggestion, I've updated the list!

Collapse
 
sebring profile image
J. G. Sebring

I'd like a way to flag pure functions like an annotation or similar. Last time I looked this up I didn't find any so I'm throwing this out - anyone doing this, and how?

example, jsdoc style

/**
* @pure
*/
add (x, y) {
 return x + y
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mathias54 profile image
Mathias Gheno Azzolini

I would love that too.

Collapse
 
nikhils4 profile image
Nikhil Singh

Great article! I would like to add that writing a pure function is more about how much control/ confidence you have on that piece of code, rather than following a bunch of rules and assuming it as a pure function. By confidence/ control, I mean how sure are you that if a certain input is any given later in time, that piece of code will give the same output.

That's how I see pure functions.

Collapse
 
brad_beggs profile image
Brad Beggs

And in particular, you would need to assign the result of using the rest operator to a new variable get the sorted array?

For example:
const array = [1,2,6,5,4]
let newArray = [...array].sort((a,b) => a-b )

console.log(newArray) // [1, 2, 4, 5, 6]
console.log(array) // [1,2,6,5,4]

Collapse
 
sanspanic profile image
Sandra Spanik • Edited

Great, I want to be exact in the language I use so thank you for commenting :) I edited “considered pure” to “typically associated with pure functions”.

As for the definition, I found it elsewhere. Before I update it, let me try and see if I’m parsing what you’re saying correctly: you’re saying that while my definition describes some side-effects, it doesn’t describe ALL side-effects, because there are some which are indeed related to computing the final output. Is that right?

Collapse
 
hkly profile image
hkly

Great write up, educational and entertaining! Definitley going to be using the Slytherin reference re function purity now with my teammates lol

Collapse
 
ash_bergs profile image
Ash

Great lunchtime read! Thanks for the well-researched article!

Collapse
 
saroj8455 profile image
Saroj Padhan

Thanks

Collapse
 
naman107 profile image
NamanAgarwal_107

So cool man! Thanks. Best article on web.

Collapse
 
electrocucaracha profile image
Victor Morales

Ansible refers pure functions as Idempotent Playbooks which are useful for retries

Collapse
 
balasundarn profile image
Balasundar

Nice article