What the heck is functional programming?
For starters, functional programming is nothing but programming that focus on using functions to build what we needed to build (e.g. softwares).
Let's understand programming is nothing but our tools and language to solve the problem using computer, as if someone needs to reach London from New Delhi, there are multiple routes. So, to solve problems there can be different approaches or in our case different types of programming.
Imperative programming
Declarative programming
Logic programming
Event-driven programming
Concurrent programming
We are not going to deep dive in any of them, but let me know if you want me to cover them as well.
Now, in Functional programming, functions are treated as first class citizens.
Wait.!!
What does that even mean, we as a human being are not treated 'first class' in our home. What does it mean for a function to be treated as 'first class' ?
It turns out, in programming, first-class means they can be assigned to variables, passed as arguement to other functions and returned as values from functions.
Here is the example for the above statement:
Key principles of Functional programming
Immutability: In simple words, "You don't change the data given to you, instead you create a new copy."
It means that once a value is assigned to a variable, it cannot be changed. Instead of modifying data in place, functional programming emphasizes creating new data structures that are derived from the old ones.Consider this:
A object
person
is created with properties name and score as34
.
Another objectanotherPerson
is set to be equal toperson
object, thenanotherPerson.marks
is assigned score as45
.
This should be changing onlyanotherPerson
and notperson
. This is doing so, because when you equate (=) one object to another, they both points to same memory location. This leads to change in Original data given to us thus contradicting the principle of immutability.What can be done instead to not let value of score spread (pun intended) to another object?
The person
object could be spreaded inside anotherPerson
and score
can be reassigned to 45, this will not result in any change in person
i.e. immutability and at the same time changing score
in anotherPerson
.
Siginificance of Immutability:
It makes code easier to understand and more predictable.
It can improve performance, especially when dealing with large amounts of data.
It facilitates functional programming and referential transparency.
Pure Function: What is a pure function? A function is called 'pure' when for the same input, it always gives the same output.
Then aren't that's what all functions do, then all functions are pure functions? NO.Some functions may have side effects, such as modifying external state or performing input/output operations, which can cause the function to produce different results for the same input.
For example, a function that generates random numbers or a
fetch()
function.
This function usesfetch()
to make an HTTP request to an external server, the response from the server can vary depending on factors outside of the function's control, such as network conditions or changes to the server's data. Therefore, the output of the function can change for the same input (i.e. the same city), making it impure.Three rules for a function to be called 'pure' :
At least one argument
return a value or other function
Should not mutate any of its arguments —> means, it should Make a new copy. A function taking an object as an argument and modifying it within the function body would not be considered as 'pure'.
It's generally a good practice to write functions that are as pure as possible, since they are easier to reason about, test, and compose into larger programs.