Skip to main content

Pure Function vs Side Effects

· 5 min read
Hassan Ali
Front End Engineer

Pure Function

Pure Function can simple function take input as a params and return the something by doing some computational. It does not communicate to the outside the world i mean console something, api calling ,getting date from outside the world.If the function can communicate to the outside the world then its not a pure function then those function is a side effect.

info
  1. Pure Function take more then one parameters and return.
  2. Printing a string like that to the console is a side effect.
  3. Avoid side effects (do nothing but return output based on nothing but input)
  4. A pure function is deterministic,meaning it's output is totally determined by its inputs.And you will always get the same result when you call the function with the samearguments.
  5. No input and no output for the not pure function, interesting.So no input, meaning there it takes no arguments.
  6. Notice that if there's no return statement in the not pure greet function.But if we don't have a return statement, that probably means that this function is doing something other than returning its value.
  7. Patrick raised the point.A pure function, if it's not allowed to do anything in the world except return its output value, it doesn't really do anything.I don't get any log output, I don't get my profile picture updated on the website.

A pure function has two characteristics:

  1. No Side Effects: A pure function has no effect on the program or the world besides outputting its return value
  2. Deterministic: Given the same input values, a pure function will always return the same output. This is because its return value depends only on its input parameters, and not on any other information (e.g. global program state)

What are the banefits of using functional programming the pure function

When we're doing functional programming,we are concerned with the kind of computational aspect of our programs.

  • Give it a string, see if it gives you the back of the right string back.
  • Give it an array of numbers,see if it gives you the right different array of numbers back.
  • It much easier to test our code and to maintain our code and to think about the interactions between different parts of our program because they each become kind of these isolated pure deterministic units.

Advantages of functional Programming

Essentially, it means that, anything going on inside of your program,all the logic, all of the business logic.And the computations and the manipulations of data thatare usually the tricky parts of getting those programs right,all of that becomes much more easier to deal with.Because it is more predictable, it's safer, and easy to test.

Example

  • Less opportunity for bugs to arise.because it depends on the input params not on the global variables.
  • It's safer.
  • it's more dependable.
  • you could say because of that determinism.
  • That means, it's also much easier to test and to debug and to find places wherethere are problems because If you've ever tested a function that's not pure.
function greet(greeting, name) {
return `${greeting}, ${name}!`;
}

greet("Hi", "Alonzo");
// "Hi, Alonzo!"

greet("Howdy", "Alan");
// "Howdy, Alan!

Does this mean calling a function within a function makes it not pure?

Very interesting question.So if you call a function from within another function,does that make the outer function impure?So in functional programming, when we're doing it really,really deliberately, we would probably be passing in pretty much everything we need to work with as an argument to the function. we're working in a functional language that forces all of ourfunctions to be pure.If we know that, that inner function is pure, andthat we're basically delegating some computation to it,then that doesn't necessarily mean, that we've got an impure function.

What is Side Effects?

Side Effects Example

  • logging to the console,which is changing something in the outside world.
  • Printing a string like that to the console is a side effect.
  • yes, no return statement or not returning a value, huge red flag.
  • But especially keeping in mind that, logging orupdating the world in any way other than returning a value that isa violation of what we set up here function is.And so, those are some great red flags for identifying a not pure function. It is a side effect.
let thesis = { name: "Church's", date: 1936 };

function renameThesis(newName) {
thesis.name = newName;
console.log("Renamed!");
}

renameThesis("Church-Turing"); // Renamed!
thesis; //{name: "Church-Turing", date: 1936}

No side effects Example

const thesis = { name: "Church's", date: 1936 };

function renameThesis(oldThesis, newName) {
return {
name: newName,
date: oldThesis.date,
};
}

const thesis2 = renameThesis(thesis, "Church-Turing");
thesis; // {name: "Church's", date: 1936}
thesis2; // {name: "Church-Turing", date: 1936}

Exercise