A programming paradigm are methods and thinking that went behind designing and implementing programs using features of a particular programming language.
There are many different programming paradigms but we’ll mainly look at the important two.
Procedural Programming Paradigm
As the name suggests, this type of programming paradigm works on procedures i.e. functions.
It is also called a top-down paradigm and it relies on a sequential execution of statements clubbed inside various small functions.
It treats data and the functions that act on those data as a same unit rather than different.
It poses some disadvantages like
- Code reusability is difficult
- It is difficult to maintain code
- Programs tend to get lengthy
Object-Oriented Programming Paradigm
This paradigm uses objects and classes as the key idea behind designing programs rather than just using procedures.
An object is a real world identifiable entity with some characteristics and behavior and a class is the object’s blueprint.
Here the data is kept different from the implementations working on them and additional features working on that data can be added very easily.
It overcomes the disadvantages of procedural programming.
Some basic advantages of OOP paradigm are
- It makes code reusable and easy to maintain
- It a data is changed, it is not changed in all the procedures, rather generally data is changed only at one place
Let’s look at a simple example and try to understand the difference between the above two concepts.
Suppose we have an oven that has various properties of its own (color, how it works, etc.) and we have a dish who has properties on its own (like ingredients, recipe, etc.).
Using procedural programming, it looks something like this
cook (oven, dish)
All the information about oven and dish is sent into this procedure and the action is carried out.
Now suppose there is another action cleaning of oven, in procedural programming it looks like
All the information about the oven is copied and again passed to this procedure.
Suppose there are 100s of different procedures that use oven, dish, and other parameters in different ways.
All contain the same information about oven again and again.
This results in code duplication which is not a good programming practice and in turn makes programs longer hence difficult to maintain.
Also if we want to change some information about oven, we have to change it at all places.
So much unnecessary work.
But if we use OOP paradigm our work can be made much easier.
As we know that OOP mainly works on objects, we can create objects for oven and dish and encapsulate there information is one place and make methods (procedures) that work on this information and carry out action like
oven.cook (dish); oven.clean()``` Here information about oven is created only once and both of these methods act on that information only, rather than creating multiple copies of same information. Also, if something is to be changed about oven, it only has to be changed at a single place! **OOP is very powerful and much better than trivial procedural programming**