We can consider Programming Paradigms as a categorization of Programming Languages based on their features. There are multiple paradigms. Some paradigms are mainly concerned about style of grammar and syntax , Some paradigms are concerned about grouping units of code along with its state. Some paradigms are concerned with implementing the execution model of the language.
The most common Programming Paradigms include :
- Imperative Paradigm : In this paradigm the programmer instructs the machine about what to do and also instructs the machine how to do that task.
Example : Let’s say we have a friend name John. He is new to the city and doesn’t know the places very well. So let’s say we want him to get a meal for us from our desired restaurant.
We instruct him like this :
1. Find a bus which goes on the route of the restaurant.
2. Then step out from the bus and head straight along the street
3. Go right of the school you see.
4. Enter the restaurant named XYZ and see the menu
5. Inside the menu, If you see find fried rice and beef sizzling and vegetables call the waiter
6. After the waiter arrives, place the order of those foods.
7. After order gets delivered pay the amount
8. Return to Home.
So basically we can see that we have instructed our dear friend John that we want him to get a meal for us from our desired restaurant XYZ and we also instructed him in detail how he can get the meal from our desired restaurant.
In the above example, If we consider John as our computer . Then we can see that we instruct our computer what to do and how to do. So this feature or this way is called imperative paradigm.
There are two sub categories in imperative paradigm -
- Procedural Programming : For example, We have a single page for writing codes to solve a problem. We just keep writing the instructions line by line. No organizing of the code nor any kind of logical groups are present in the style of our code that kind of style is called procedural programming.
- Object Oriented Programming (OOP) : Opposite to what we saw in procedural programming, in object oriented programming we organize the codes in logical groups along with its state. In this way every unit of instruction is separated from one another and we can reuse a single unit . This paradigm follows KIS (Keep It Simple) principle and DRY (Don’t Repeat Yourself) Principle.
As an example of Object Oriented programming we can use our previous example with John :
So up until now we have instructed john line by line what to do, right? So when we are in Object Oriented Paradigm , we can divide those instructions in units of code like this:
We can have a class called Transport. It will have some properties to describe the class and a method called transport() which will contain all the instructions of transporting to the restaurant.
1. Find a bus which goes on the route of the restaurant.
2. Then step out from the bus and head straight along the street
3. Go right of the school you see.
4. Enter the restaurant named XYZ and see the menu
This block of instructions will only be present inside the transport() method.
We will have another class called Order. It will also have some properties to describe the class and a method called orderFood() which will contain all the instruction of ordering food.
1. Inside the menu, If you see find fried rice and beef sizzling and vegetables call the waiter
2. After the waiter arrives, place the order of those foods.
3. After order gets delivered pay the amount
This block of instructions will only be present inside the orderFood() method.
So now in the single page we do not need to write all the codes in one place line by line. Right now If we just call the methods then our problem will be solved. Moreover in future if we have another friend named Mike who resides in our city and knows the way to the restaurant then Mike will not need to know the transportation instructions. He will only need the order instructions. So we can just pass him our orderFood() method and the problem will be solved.
Though Object Oriented programming solves lot of our problems and make it easier to code and manage. It also has some problem because in the end Object Oriented programming is a sub category of Imperative Paradigm. That’s why need to write lot of codes. We need to write logics of some basic functionalities repeatedly but we don’t really need to know about those logics at all.
To solve this kind of problems another paradigm is introduced which is called declarative paradigm.
- Declarative Paradigm : Declarative Paradigm is a programming paradigm that expresses the logic of a computation without describing its control flow.
Well, If I simply want to define declarative paradigm with an example , Then I can say :
In this paradigm , a programmer only instructs the computer about what to do. In this paradigm programmer do not have to instruct the computer in detail that how to do the task.
Let me clear the concept with the example of our dear friend John :
So, in declarative paradigm we just instruct John that “Hey John! We need a lunch from XYZ restaurant”. This is our only instruction to John. Like Imperative Paradigm we do not instruct John that how to go to the restaurant and how to order food and how to return home. We don’t share anything except what to do. We don’t tell him how to complete the task.
We can also consider HTML as a declarative paradigm language. because in HTML , there are lots of tags right? So for example If we write a h1 tag we can see that a large heading is automatically printed on the screen. but we don’t tell the computer how to print the heading on the screen. We just write what we need (h1) and we can see the result on screen.
However, someone already wrote the detailed instruction behind the scene of our instruction. Someone already wrote the detailed instruction in imperative paradigm behind the scene of what we are instructing to the computer.
Let me share another example to clear the concept :
We are going to iterate a sample array in both traditional way and modern way -
const arr = [1, 2, 3, 4, 5, 6];
// Traditional way of iterating an array
for(let i = 0; i < arr.length; i++){
console.log(arr[i]);
}
// Modern way of iterating an array
for(const element of arr) {
console.log(element);
}
So in the above example , we can see the both traditional way and modern way of iterating an array. In traditional way we can see that we are instructing our computer in detail to iterate an array but in modern way it’s completely different. We are just running a for of loop and the magic happens . We do not need to write detail code about how to iterate an array.
So like this , someone already wrote the detailed code in imperative paradigm behind the scenes of for of loop and of course for of loop is an example of declarative paradigm.
Now we understand what is declarative paradigm. Like imperative paradigm the declarative paradigm has also four sub categories which are given below -
- Functional Programming : Functional programming is a programming paradigm which builds a program by applying and composing functions. In functional programming , Functions are treated as first class functions where a single function can be passed as argument in a function, can be returned in a function and can be used as a variable.
We need to note one more thing about Functional Programming and that is Functional Programming has two categories .
Pure Functional Programming : This type of programming contains only functional programming. It has no side effects or it does not support any other paradigms.
Impure Functional Programming : This type of programming contains multiple paradigm and hence it is called Impure Functional Programming.
- Logic Programming : Logic Programming is a programming paradigm that is mainly used to design system logic.
- Mathematical Programming : Mathematical Programming is a programming paradigm that is used to provide solution to a optimization problem.
- Reactive Programming : Reactive Programming is a programming paradigm that is used to declare a result with data streams and propagation of change.