As there are many programming languages, there are many programming paradigms.
Programming paradigm = style of programming = set of guidelines to solve the problem.
So, many programming languages evolved initially with specific problems to solve, and hence each programming language has been developed with a specific paradigm in mind(like Java is based on an Object-oriented paradigm because its history traces back to electronic devices)
Programming languages have become multi-paradigm (i.e., support many paradigms like Javascript and Python) over the period.
Points to remember:
Languages and paradigms are different, even though languages are born with specific problems in mind.
Modern languages are now multi-paradigm.
We will cover some popular paradigms and ensure nothing seems Greek to you when somebody speaks. Lol!
Imperative programming:
You will give a set of instructions in a particular order to the computer, and the computer executes it.
Say you want to prepare a filter coffee for a guest. In the imperative paradigm, you must say from "taking utensils, pouring decoction, heating to final serving to the guest" in sequential order.
Imperative = Detailed instructions and implementation details are instructed in the correct order.
Procedural programming:
It is derived from imperative, and I can say the next evolution step of imperative.
In the imperative approach, every time you prepare coffee, you must tell the steps/instructions from start to end.
So what if these common instructions can be reused? This is where functions or modularity come in.
Utensils instruction can be stored in one module.
Heating in one module.
Mixing of ingredients in another module etc.
So every time a new guest, you have to see the stored instructions in the module or function and use it.
The abstraction here is one of the benefits, but still, we need to write a set of instructions inside functions.
Functional programming:
This takes the functions to another level. In the procedural paradigm, we store the instructions.
In functional programming, we can treat functions as "first-class citizens" - they can be assigned to variables, passing functions as parameters and returning another function.
Here pure functions come,
Pure functions take in input and give output just like all functions, but it doesn't change anything outside the function scope.
const arr = [1,2,3,4,5];
const res = [];
function multiplyBy2(arr) {
for(let i=0;i<arr.length;i++) {
let byTwo = arr[i] * 2;
res.push(byTwo)
}
return res
}
Is the above code pure functions? Pause and ponder!
No! because it manipulates the variable "res" outside the function.
The pure function must give the same output for the same input.
If I change the res to "res = [100,20] in the above code outside the function, the output of the function will obviously change. So it's an impure function.
We will make it pure,
const arr = [1,2,3,4,5];
function multiplyBy2(arr) {
const res = [];
for(let i=0;i<arr.length;i++) {
let byTwo = arr[i] * 2;
res.push(byTwo)
}
return res
}
Now "res is inside the function" can't be manipulated outside. So it's pure function.
Any change outside the function is called a "side effect."
So functional programming provides you - modularity, no "side effects," giving single responsibility to every function, and hence more security.
Note: I am using a JS example here, and in JS, each creates its own execution context, and variables have closures(Can't be accessed outside). So it is pure function!
Declarative programming:
This is more about moving towards human friendliness. In this approach, we abstract most of the implementation details and focus more on the result than the implementation details.
This we use mostly in today's world, taking the same code above, and the result we want is multiplied by 2.
const arr = [1,2,3,4,5];
const res = arr.map(ele => ele * 2);
console.log(res);
See here. A "map" method abstracts away the loop. And it is provided by Javascript itself as a feature for arrays.
This is the next level of abstraction through Declarative programming, and it is most widely used in modern programming languages.
Object-oriented paradigm
Real-world objects heavily inspire this. We have data and do operations on data. We essentially do this in programming at the end of the day.
OOPS is a paradigm that uses abstraction, inheritance, encapsulation, etc, in the code arrangement.
There are many aspects or perspectives through which we explain the object-oriented paradigm. Many good explanations you can find on the web.
But for the continuity of this article, I would love to extend from previous paradigms.
I stressed more pure functions in the functional paradigm --> same output for the same input.
A function can return anything and takes in anything if it is first-class citizens.
Note: Older programming languages don't treat functions as first class, and OOPS traces back to the older period.
In the pre-first class era, say imperative or procedural,if you want create a zoo in which many animals live, you will have to write separate instructions or group instructions in some module for every animal in the zoo.
For every animal --> we write separate code --> No reusability of code!
If functions can be treated as first-class citizens, it can take anything as parameters and returns anything as output.
So we can create a function called Animal by calling the functions with different parameters every time the function outputs the corresponding animal. These functions are called "Factory functions."
This Factory functions are like templates or blue prints for animals.
The parameters we pass to this function for creating animals are called attributes or properties of animals.
This blueprint function on a meta-level is called a Class, and the animals we create from this class are called instances or Objects.
Why meta level?
Because Class may contain functions other than properties/attributes, these functions are called methods. Say we need to define the behavior of animals, like how they sound, walk, run, etc.
These methods perform operations on properties.
There are many advantages associated with the OOP paradigm, which is beyond this article's scope.
Please watch this video for comparison: Link. It gives you a clear and concise explanation and comparison.