Procedural Vs Object-Oriented Coding Style

Yashshavi Kashyap
The Startup
Published in
6 min readAug 15, 2020

--

I always had a myth in my mind where I used to think that if I am using classes in my code then I am using Object Oriented approach but at some point I found out that I was wrong.

Recently, while I was reading book “Clean Code” by Robert C. Martin, I have gone through some examples which took me by surprise. There were some examples used for demonstrating the procedural coding style and OO coding style and this took me by surprise because both the examples were using classes and methods. So I read it and learned some very good insight. In this article, I would explain what I learned after all that time.

Procedural Programming-

Wikipedia defines procedural programming as:​​​​​​​

From this definition, it is asserted that procedural programming is really just the act of specifying a set of ordered steps needed to implement the requested functionality. It also implies about how those steps are implemented is a detail that’s not related to the paradigm. The important thing is that it’s imperative in how it works.

Let’s look at a few examples:

The below code is procedural-

Procedural approach for solving problem

In a procedural programming language, a program basically consists of a sequence of instructions each of which tells the computer to do something such as we are doing it, and functions are written for the accomplishment of a specific tasks, here in this example we have created different methods for calculating areas of different geometries.

Now the approach will remain procedural, even if it uses an object:

Procedural approach for solving problem using objects

Now, also even though we are using object the paradigm we are following is same. We still have sequence of instructions, and functions for accomplishing different tasks.

This is still procedural, even though it uses a class:

Procedural approach for solving problem using classes

Now, though we have created the class for different geometry but still it is procedural. Procedural programming doesn’t have to do anything with whether we use classes or not, it’s a programming approach.

The three codes mentioned above use the same programming approach. The only difference between them is the way the routines are resolved but each code is procedural. Each one has discrete steps that must be taken into consideration.

Let’s take a look at how the OOP approach for this program will look like –

Object Oriented Programming-

According to Wikipedia, Object Oriented Programming is — ​​​​​​​

The concepts that we can take it out from here are –

  • It must abstract the data concepts into modular units.​​​​​​​
  • It must have some polymorphic way to execute code.
  • It must at least partially encapsulate that code and functionality.

Now let’s implement these concepts on above example –

Object Oriented approach for problem solving

What‘s the difference?

Objects used in OOP hide the data behind abstractions and expose functions that operate on them.

In the first procedural programming approach, classes Square, Rectangle and Circle are exposing their data attributes, “side”, “height and width”, and “center and radius” respectively. The class Geometry can directly work and access their data attributes. Indeed it would expose implementation even if the variables were private and we were using variable getters and setters.

Hiding implementation is not just putting a layer of functions between the variables. Hiding implementations is about abstracting behavior. A class does not push its variables out through getters and setters. Instead, it exposes abstract interfaces that allows its users to manipulate the essence of data, without even knowing its implementation. As it is implemented in OOP approach example, each class has its own area functions. Now, no class other than the respective shape class knows about the implementation of calculating area. The classes are exposing “area ()” function that operate on their respective data attributes and provides the resulted area.

OOP Problem solving approach Vs Procedural problem solving approach

Now let’s go back to OOP example and revise the concepts that we have taken out from that definition of Wikipedia for OOP.

  • The OOP approach example is abstracting the data concepts into modular units.
  • The OOP approach example is using the “area ()” method polymorphically.
  • And the data and the methods are encapsulated together.

So if someone is using object, it should not be assumed that he is writing OOP and because someone is using functions, they are using procedural programming, which is not true.

What to use when?

Now suppose we want to calculate Parameters also, then the respective shape (Circle, Square, and Rectangle) will be unaffected and any other class that depends on them will also remain unaffected. But if I add a new class for shape like Triangle, then I must change all the functions in Geometry to deal with it. However, in case of OPP approach example none of the existing functions are effected. But if I add a new function all of the shape class will change.

There is a complimentary nature in these two approaches, as it is summarized in Clean Code book by Robert C. Martin -

Procedural code make it easy to add new functions without changing the existing data structures, OO Code, on the other hand, make it easy to add new classes without changing existing functions.

So, we can conclude that when we want to add new data types rather than new functions, for these cases OO approach is appropriate. Whereas, when we want to add new functions as opposed to data types, procedural code is more appropriate.

Other aspects to consider include performance vs maintainability. If one intend on having a specialized program that won’t have change requirements and is designed for high performance, their efforts will be better served when building with PP concepts. A short script like programs that run against crontab, windows Powershell, or Launchctl shines here.

Conclusion

Procedural vs OOP is an approach to writing code, not how one writes it. If someone is focusing on “Steps” and an ordered way of writing a program then he is more likely writing a procedural code. But if someone is focusing on state transformations and encapsulated abstractions, he is writing OOP.

The choice of usage from these approaches depends upon the application we want to write. It depends upon the aspects such as Procedural performance, maintainability, re-usability etc.

Thanks for reading this article.

If you have any additional explanations, or any comments please do not hesitate to add them below. I will be glad to know them.

--

--

Yashshavi Kashyap
The Startup

Creative thinker and always a learner, currently working as software engineer...