DEV Community

Cover image for Object Oriented Programming (OOP) x Procedural Programming (PP)
Thomas Ribeiro
Thomas Ribeiro

Posted on

Object Oriented Programming (OOP) x Procedural Programming (PP)

Whatever the time, at some point in our career as developers we came to a discussion about programming language paradigms. And as it is crucial to understand more about the advantages that a certain language can bring us in order to make the most advantageous choice for our project, understanding the differences and advantages of programming paradigms will help us a lot in the process. It was exactly with this in mind that I decided to write this post explaining a little about the difference between the two.

Procedural Programming

Procedural Programming (PC) or also called Structured Programming concerns modularizing instructions for executing a program into procedures or parts. Procedures contain a series of computational steps to be executed. A procedure can be called at any time during the execution of a program, including by itself or by other procedures. Procedures can receive arguments (parameters) to perform manipulations and may or may not return values, and unlike the concept of functional programming, procedures can also cause side effects like object methods. To simplify the difference in concepts between a procedure and a function is that a function would always return some value and would not change any arguments outside its scope, whereas procedures can change something outside their scope and do not necessarily return any value. This change of data outside its scope which are named as side effects.

Object-oriented programming

Object Oriented Programming (OOP) is based on the idea that everything is classes and objects, classes are like groupers and objects are the participants of these groups. Each class defines attributes (characteristics) and methods (behaviors) that are common to everyone belonging to this class, in this case to each object that is an instance of the class. Objects can have an attribute that is equal to all belonging to the class, which are called class attributes, or attributes that despite defining the same thing do not have the same value, called object attributes, such as:

  • Someone's age, every person has an age, but not necessarily every person has the same age.

Methods are abstractions to hide the details of objects, exposing only the functions that operate their data.
Another very important concept in the world of OOP is polymorphism, which is basically the beginning of which classes derived from the same superclass (parent class) expose methods that have the same identification but with different implementations, as each class has its specificity and Because of this, different ways to perform the same action.

For example, let's imagine the Animal superclass, it defines that all its derivatives must have the emitSound method, the derived class Dog implements the emitSound method which would be barking, while the Cat class implements emitSound as meowing. However, this internal behavior does not matter to those who see the class from the outside.

public interface Animal {
    public void emitSound();
}
Enter fullscreen mode Exit fullscreen mode
public class Cat implements Animal {
    @Override
    public void emitSound() {
        System.out.println("Meow");
    }
}
Enter fullscreen mode Exit fullscreen mode
public class Dog implements Animal {
    @Override
    public void emitSound() {
        System.out.println("Bark");
    }
}
Enter fullscreen mode Exit fullscreen mode

Regarding the differences, in my opinion, one of the most obvious is the way in which the data is treated, in OOP we manipulate the objects but we do not have direct access to the values, in PP it is the opposite.

According to Robert Cecil Martin, author of the book Clean Code:

Data structures expose your data and do not have significant functions for manipulating it.

Therefore, everything will depend on the way and objective you need to achieve the objective and solve the problem in your current context.

Top comments (0)