Why we will use Object Oriented Programming

By | September 20, 2018

Before OOP programmer were used Procedural programming where code were written in linear fashion i.e. from top to bottom. So when program became larger then programmer faced huge problem to manage and debug the code.

So to get rid from such problem taken an attempt and introduced function- break down code into manageable segments. It is commonly known as functional programming.

But as program performed more complex business functionality and interacted with other system programmer faced four basic problems those are-

1. Reusability

2. Extensibility

3. Simplicity

4. Maintainability

Let’s discuss about details of each above problems-

1. Reusability Problem

If programmer writes some code for handle certain purpose most of the time they can’t reuse it at another place whenever they need to use it. Let’s consider following example-

  • We have a Product screen as follows-

This is a single entry form i.e. we will be entry single product at a time.

Here when we have to calculate the Total Cost of each product based on Product Quantity and Per Product Cost. The calculation is as follows-

Total Cost = Product Quantity * Per Product Cost

  • We have handled the calculation inside a function as follows-
private int CalculateTotalCost(int unitPrice, int quantity)
{
    return unitPrice * quantity;
} 

Inside above function we have unitPrice and quantity and to calculate the total price we multiplied unitPrice and quantity and returned the result.

  • Now we will also have a multiple entry form as follows-

This is also a Product entry form. In this form we also need to calcualte the total Price based on the Per Product Cost and Product Quantity as above mentioned formula. So we need to reuse previous function i.e. we need to use CalculateTotalCost() method here. But we can’t reuse it. So in general case to use above function we do copy the function from single entry form and paste it to multiple entry form.

  • So both of above page has two copies of same code that increases the redundancy and there is no re-usability option of same code.

So in above case we are not reuse the existing code instead we are copy and paste same code in different places (at single Product entry form & multiple product entry form) and it actually make our code redundant.

2. Extensibility Problem

The second major problem of functional programming is extensibility. We can’t extend any method in functional programming. Let’s see following example-

In above product screen we need to introduce discount. In functional programming there is no way to implement discount by extending CalculateTotalCost() method. Here what we will do is-

Create another method called CalculateTotalCostIncludingDiscount() as follows-

private int CalculateTotalCostIncludingDiscount(int unitPrice, int quantity, int discount)
{
    return CalculateTotalCost(unitPrice, quantity) - discount;
} 

Here we created a brand new function and passed unitPrice, quantity and discount as parameter. Inside the function we called CalculateTotalCost() method and subtracted discount from total cost and finally returned the result.

So we again have to copy paste the existing function to new function and then add new logic i.e. discount logic to it.

So again we have redundant code because in functional programming there is no way to take current function and extend it.

3. Simplicity

Simple is beauty. People always love simple things. People always feel comfort with simple anything. In functional programming though there is no option for reuse and extend the existing code, so by passing of time and increasing of logics the code becomes cluttered and scattered. And finally the code loses its simplicity and become more and more complex.

4. Maintainability

Though in functional programming there is no option for reusability, extensibility and simplicity so as the code becomes larger it loses its maintainability state i.e. programmer loose total control from the code and become hard to maintain. New bugs are introducing frequently and programmers start nightmare during handling those bugs and by passing of time the code and the system will doom sooner.

67 Total Views 1 Views Today
Md. Mojammel Haque

CSM, CSPO, CSD, CSP-SM, CSP-PO (ScrumAlliance.org)
Certification Profile Link-
https://www.scrumalliance.org/community/profile/mhaque13

Currently working as Lead Team (Application Architecture) at Raven Systems Ltd. Passion for software development especially agile practices such as TDD with in depth knowledge of Object Oriented Programming, SOLID Principles, Gang of Four Design Patterns, Some Enterprise Application Architectural Patterns. Over 8 years of software development experience ASP.NET. Has the ability to understand and transform complex business requirements into software ensuring applications are delivered on time. Also experience in non Microsoft .NET technologies such as Dapper.Net, Git, Structure Map & Angular, Bootstrap, HTML-5, CSS-3 etc.

Category: 1.08-Object Oriented Programming
Md. Mojammel Haque

About Md. Mojammel Haque

CSM, CSPO, CSD, CSP-SM, CSP-PO (ScrumAlliance.org) Certification Profile Link- https://www.scrumalliance.org/community/profile/mhaque13 Currently working as Lead Team (Application Architecture) at Raven Systems Ltd. Passion for software development especially agile practices such as TDD with in depth knowledge of Object Oriented Programming, SOLID Principles, Gang of Four Design Patterns, Some Enterprise Application Architectural Patterns. Over 8 years of software development experience ASP.NET. Has the ability to understand and transform complex business requirements into software ensuring applications are delivered on time. Also experience in non Microsoft .NET technologies such as Dapper.Net, Git, Structure Map & Angular, Bootstrap, HTML-5, CSS-3 etc.

Leave a Reply

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