Basic Intro to Functional Programming

ยท

3 min read

Functional Programming Background

Functional programming is a programming paradigm that avoids shared state, mutability and side effects; this is done by using pure functions. Because functional programming aims to avoid side effects and mutability it makes unit testing easier.

Huh? A Pure Function? What is that?

In simple terms, a pure function will always return the same result if the inputs are the same AND the function does not have any side effects.

I Still Don't Understand

Okay, okay let's have a look at some code. This first example is a pure function, it has no side effects and given the same inputs it will always return the same output.

const sum = (a, b) => a + b;

sum(1, 4); // the answers 5
sum(1, 4); // the answers still 5
sum(1, 4); // guess what... its still 5

Lets now have a look at an impure function and see if we can figure out what is going to be returned

const randSum = (a, b) => a + b + Math.random() * 10;

randSum(1, 4); // what will be returned?
randSum(1, 4); // whats going to be returned this time?
randSum(1, 4); // surely you can get one out of three right?

So why is this an impure function? Simple, each time the function is called with the same inputs, the output is going to be different; this makes it unpredictable and therefore difficult to unit test.

Basics Out of the Way

Okay, we've got through the simple pure and impure functions, lets look at another function and let's figure out if it's pure or impure.

const delta = 5;
const deltaSum =  (a,b) => {
    delta = 4;
    return a + b + delta;
}

deltaSum(5,6); // returns 15
deltaSum(5,6); // still returns 15

So when the inputs are the same, the value returned is always the same so surely this is a pure function right?

Wrong!

This function is also impure as the function relies on a global variable and the function has side effects by changing (or mutating if you prefer) the global variable from inside the function.

Other examples of impure functions

  • Using console.log() inside of a function makes the function impure as this is an external API and not a javascript method.
  • Throwing exceptions makes the function impure because our function now has a side effect

Time To Be Realistic

It's all well and good on paper saying that every function needs to be pure but in reality, we need to write to the console, we need to throw exceptions and we might even have to mutate state occasionally.

So what can we do?

We can create a clear boundary between the parts of our code that are pure and which are impure; this lets us and any other developers know what to expect.

Overall

This is not everything that makes up functional programming but just a single part. Functional programming certainly has its place and it makes predicting and unit testing code a lot easier. I would recommend reading up on functional programming if you are eager to learn new things and broaden your horizons.

You Da Best

I appreciate you reading this article and I hope you learned something new! If did enjoy the article I'd appreciate your sharing and adding a reaction on the article ๐Ÿ‘

Did you find this article valuable?

Support Thoughts of a developer by becoming a sponsor. Any amount is appreciated!

ย