Tomblog

Another crappy blog no one reads

Why do we need OOP?

As a computer science student I hear a lot of complaints about Object Oriented Programming(oop), most of it seems to come from people who just don’t seem to understand what it’s is good for or what it is used for. So, as a public service to those people, I’m writing this. It’s as through of an explanation of the reasons for oop as I can write, well more accurately as I care to write. What this is not is not is an explanation of how to use oop, if you want that you can look elsewhere.

There are lots of complaints about oop, mostly from newer programmers and people who have to write small pieces of code. Most people don’t like that you can’t manipulate variables directly, why write extra methods to get and set data when you can just do it directly? Others don’t understand the point of all the layers of abstraction called for in oop. These things are all in oop for a reason, and understanding the reasons is an important part of being a programmer.

OOP is a big step up from functional when it comes to writing dependable, reusable and maintainable code. All of these things should be important to any one writing code, of any complexity or size. In the dark days code was written in a task oriented way. You wrote functions to do a job and then put those functions together to make your program. While this is capable of making any program you may want to make, it’s also very clumsy and error prone. There are lots of variables in the program that are available to functions that have no business modifying them or even reading them. While this is normally not a problem for one person writing code, very few programs are written by individuals. Even if the program was written by one person there is always the chance that someone else will be maintaining the code and could accidentally modify variables incorrectly leading to bugs. This is one of the benefits of oop.

In oop all data is encapsulated and control of data members is protected by the class, in this manner the programmer has control over how the data is accessed and manipulated, if someone wants to put a string of length five into a variable that is meant to hold a phone number the programmer can check for these things and take want ever action he deems appropriate. In functional programming the only option the programmer had was to check of the data was right at the beginning of the function which is a waste of resources and will lead to a lot of repeated code for functions that use the same data. It would obviously be better if we could ensure the data was right when we changed the variable and knew that the data has not been changed since it was set.

Another problem that oop solves is code re usability. Since code must be broken into small, discrete parts to be put into an object oriented paradigm, these parts can be used in as many different programs that require the same functionality by just calling the class where appropriate and using it. With functional programming functions could be brought in, but you run the risk that you forgot to bring in functions that where needed to run the original function properly. While this risk still exists with oop it is minimized because you no longer copy paste individual functions, rather you just use the original file that contains the class and which has all includes needed to run the class.

Now that you know some good reasons to use it lets explain what oop is. Functional programming is an action based programming paradigm, in direct opposition to this oop is a thing based paradigm. So instead of writing functions to preform actions, you write classes to represent things. These things control how they interact with the rest of the program and how the rest of the program interacts with them. Certain things are just extensions of other things and should be able to do the same things as its parent…so how does this help the programmer? Well instead of writing functions and hoping that you one day find the exact situation that the function was written for, you write classes that know how to do what they are supposed to be able to do.

So is oop the end all be all of programming, no far from it. There are plenty of problems with oop, plenty of things that oop can’t do or can’t do well but for now it is a good solution, and one that works. To be perfectly honest my little cousins have been over while I was writing this and I’ve been distracted a lot…I’m not even sure if this makes sense anymore.

Tomasz Kolodziejczyk on September 23rd, 2006 | Filed under Programming | 1 Comment »

One Response to “Why do we need OOP?”

  1. Corvillus Says:

    Personally, I think object oriented programming is just the next step up from structured (functional) programming with respect to relating programming to real-world semantics. Structured programming gives you the verbs (functions), and object oriented programming simply throws the nouns (objects) and adjectives (interfaces, properties) into the mix. OOP makes it much easier to map application code to real world objects and behaviours. A well built object oriented application on all except it’s absolute lowest levels reads semantically (and in some languages, such as Ruby, even syntactically) like spoken english. Also, due to the several layers of abstraction a proper OOP infrastructure requires, you can often minimize the locations code needs to be changed, while at the same time having more versatile code (a well built object can often be used outside of the context for which it was originally designed, which is a very efficient form of code reuse).