DEV Community

Cover image for Principles of Functional Programming

Principles of Functional Programming

James Robb on February 25, 2020

Functional programming is a paradigm which has its roots in mathematics, primarily stemming from lambda calculus. Functional programming aims to be...
Collapse
 
ilya_sher_prog profile image
Ilya Sher • Edited

To fix this we turn to immutability and the use of copies to keep the initial state transparent and immutable.

Note / opinion: while use of copies serves the example (I guess), in real world one should definitely fix logElements not to modify the input parameter. Please don't leave bombs like this to your colleagues - logElements name does not suggest it would modify the elements array.

I would call the suggested "fix" a "workaround", which should only be made if you can't modify the logElements function.

Collapse
 
jamesrweb profile image
James Robb • Edited

I agree that this isn’t good code or practice to use in a production environment and if seen in the wild it should be refactored but that was the point of the example.

Furthermore the example isn’t my own and instead comes from the linked source who uses this example to showcase a similar point.

The other point of consideration is that in some environments it is unavoidable to have mutations to the inputs and so clones and copies become the default solution to avoid mutation to the original values which this example showcases.

Collapse
 
ilya_sher_prog profile image
Ilya Sher

Furthermore the example isn’t my own

Really doesn't matter. I was commenting about the example, not about you :)

The aim is to let potentially inexperienced readers know that this is typically not OK.

The other point of consideration is that in some environments it is unavoidable to have mutations to the inputs

Unfortunately, yes.

and so clones and copies become the default solution to avoid mutation to the original values which this example showcases.

I would also like the readers to understand that it's more of a workaround than a solution.

Thread Thread
 
jamesrweb profile image
James Robb • Edited

“I would also like the readers to understand that it's more of a workaround than a solution.” - only if it’s avoidable since in the environments where we can’t have proper immutability this is the solution.

Fair enough, it’s a point though. Thanks for the comments!

Thread Thread
 
ilya_sher_prog profile image
Ilya Sher

Mmm.. interesting philosophical topic. I really need to think about it. Thinking here now. I would say that it's a workaround, which is the appropriate solution for the situation (as opposed to refactoring the original function, which is not possible).

Keeping it in mind and in comments as "workaround" should potentially push more towards the "real" solution when it becomes possible.

I guess "workaround" is specific type of solution in this situation. Between two words when one is more specific, I choose the specific one unless I specifically want to generalize.

Collapse
 
pablocamacho profile image
pablocamacho

Hi, James.

While talking about "pure functions", you mentioned that the function getSquare( items ) is an impure one. Just because "(...) any function that changes its inputs or the value of some external variable is an impure function.". So, since we are receiving a container, changing its content and returning it already modified, leads to be working with an impure function.

What about if I perform an internal copy of the "items" parameter, like "items1", and then do exactly the same process your function does but over the "items1" container and finally I return "items1". Should now this be a pure function? If the answer is "no", which would be the reason? Another point here might be "because we have a loop inside it, which is prone to side effects", correct? But, if this is the case, we have another reason different from: "any function that changes its inputs or the value of some external variable is an impure function."

I have asked this same question (based on your post) to a well-known Developer, Teacher and Writer and he gave another definition to me of a "pure function": "A function should always give the same result when given the same arguments."

This definition is clearly different than yours.

Could you please clarify and explain this topic better?

Thanks in advance!

Collapse
 
jamesrweb profile image
James Robb

What about if I perform an internal copy of the "items" parameter, like "items1", and then do exactly the same process your function does but over the "items1" container and finally I return "items1". Should now this be a pure function?

As I stated in the article:

Any function that changes its inputs or the value of some external variable is an impure function.

So long as our inputs are unaffected and the output is consistent, the function is pure but if you alter variables you then break the rules of immutability but that's a different point.

If the answer is "no", which would be the reason?

If acting on an internal variable that is thrown away after execution, that is fine so long as the internal variable does not break the rules of pureness itself, for example mutating another internal variable such as:

function add(a, b) {
    let sum = a;
    sum += b;
    return sum;
}
Enter fullscreen mode Exit fullscreen mode

But it does break the rules of immutability which is why it should be avoided to do something like this. We never want to mutate variables but to create immutable deep copies or new values:

function add(a, b) {
    const start = a;
    const withB = start + b;
    return sum;
}
Enter fullscreen mode Exit fullscreen mode

This is a silly example since returning a + b would suffice but if you do need variables then immutable constants are the way to go.

Another point here might be "because we have a loop inside it, which is prone to side effects", correct? But, if this is the case, we have another reason different from: "any function that changes its inputs or the value of some external variable is an impure function."

Loops aren't inherintly bad constructs, they have uses but generally speaking whatever a loop can do, so can mappers, reducers, recursion and so on. For example in the article on the pure functions section I gave the example of calculating the squares of numbers, a pure way to do that without loops would be like so:

function getSquare(x: number): number {
   return x * x;
}

function getSquares(items: number[]): number[] {
  return items.map(getSquare);
}
Enter fullscreen mode Exit fullscreen mode

No loops required.

This definition is clearly different than yours.

Could you please clarify and explain this topic better?

As I stated in the pure functions section:

A pure function is a function which:

  1. Given the same inputs, always returns the same output
  2. Has no side-effects

The first point is the same as the definition the "well-known Developer, Teacher and Writer" stated.

If you need something else clarified though, feel free to ask!