Programming paradigms

Programming paradigms – is object-oriented programming better?

The errors faced in the procedural programming approach are the motivating factor in the invention of objected oriented approach. In the object-oriented programming paradigm, data is a critical element in the program and restricts freely transformation of data around the system.

Instead, data are associated with functions that operate on it and protect it from accidental modification outside functions. Object-oriented programming paradigm approach permits decomposition of a problem into entities, or objects and then build data and function around them. Data of an object are accessible only by the functions, or methods belonging with the object.

In this blog, we will discuss a rough concept of what Object-oriented programming paradigm is, and how it is a better solution comparing to procedural programming in some cases.

Tl; dr;

Object oriented programming is a way to write instructions for a software. It introduces 3 concepts: Encapsulation, Inheritance and Polymorphism, which form the backbone of the paradigm. There are various advantages of using OOP, as well as some drawbacks that can make us stay away from it.

Other blog posts on the series:

Programming paradigms – what is procedural programming?

 

What is the object-oriented programming paradigm?

Object-Oriented programming (OOP) is a programming paradigm that relates the structures of data with a set of operators which work with the data. In OOP, an instance of such an entity is often referred to as an object. In other words, OOP is a method of implementation in which programs consist of objects, each of which represents an instance of some classes. Classes are all members of a hierarchy of classes united through a concept called inheritance.

// a simple C++ class
class Rectangle {
  int width, height;

  public:
    void Rectangle(int x, int y) {
      width = x;
      height = y;
    }
    
    int area() { return width*height; }
};

Advantages

Eliminating redundant code using various techniques like inheritance and templates.

Imagine you need to store a list of users, which can have various roles such as admin, user, sales, etc. Instead of having to copy the duplicated fields to each user object, we can utilize a trademark feature of OOP paradigm: Inheritance

Object oriented programming visualized

class User {
  protected:
    string name;
    int age;

  public:
    void User(string n, int a) { 
      name = n; 
      age = a;
    }
};

class Admin: public User {
  public:
    bool isAccessible(string page) {
      return true; // admin should have access to everything
    }
};

class Sales: public User {
  public:
    bool isAccessible(string page) {
      return page == "sales" ? true : false;
    }
};

In the example above, we don’t have to re-declare name and age for each child class, because `User` class already provides them.

Data encapsulation

Hiding of the data enables the programmer to code the most secure programs where other parts of the program will be unable to access the hidden data. This separates the data from other logic and avoid making changes of data where inappropriate.

Code management

Division of program into objects makes software development easy.

Software complexity is less severe than conventional programming techniques.

Upgrading and maintenance of software are easily manageable.

Message passing technique makes the interface simpler with external systems.

Code reuse-ability is much easier than conventional programming languages.

Reduce code duplication

Parent class can contain similar behaviours so derived classes can use them without duplicating the logic. And if there is something different, we can simply override the method to custom tailor it to what we want. This is what polymorphism is about.

class User {
  protected:
    string name;
    int age;

  public:
    void User(string n, int a) { 
      name = n; 
      age = a;
    }

    bool isOld() {
      return age == 50 ? true : false;
    }
};

class Admin: public User {
  public:
    bool isOld() {
      // overriding the parent method
      // making admin always old :troll:
      return true;
    }
};

Disadvantages

  • Compiler and runtime overhead is high because the object-oriented program requires more time during compilation and for dynamic and runtime support it requires more resource and processing time.
  • Re-orientation of software developer to the object-oriented thinking.
  • Requires the mastery in software engineering and programming methodology
  • Benefits only in long run while managing large software projects.
  • The message passing between many objects in a complex application can be difficult to trace & debug.

Summary

Object-oriented programming is one of the most popular programming paradigm among other methodologies. Its emphasis is on data rather than procedures by dividing programs into objects. Functions and data tie together in a single unit and therefore hiding data from accidental alteration from other function or objects. Also, data access is done through visible functions so that communication between objects is possible.

By Tuan Nguyen

One thought to “Programming paradigms – is object-oriented programming better?”

Leave a Reply

Your email address will not be published. Required fields are marked *