Sunday, December 30, 2007

The Object Oriented way

At Thoughtworks, anyone who apply for the position of developer, need to submit code for a problem. I don't take part much actively in recruitment, just because yet i am not good enough for it. But still, I sometime get a chance to look at code written by people who have significant years of experience. And surprisingly, I found that
most of the time, the code written by these techie and experienced guys is pathetic. I know I am not a very good OO programmer, but I think, most of the code I saw was not OO at all. It was just a procedural code written in language like Java, C++ that supports OO.

My present project has a legacy code written in C#, which is one of the prominent OO languages. But I can bet that the legacy code in my project can be an ideal example of how to write procedural code in a OO language. It has all the problems that a typical large procedural program will have. Is witting a good (or not so good) OO program difficult ?

Most important thing that is missing is Object oriented way of thinking about problem at hand. OOP needs a way of thinking that is entirely different from procedural way.

In simple words, procedural programming says that program or a software system is a set of instructions and procedures. In procedural programming, when we have a problem to solve, we think about the procedure to solve it and then write that procedure down in form of function/method. Now the data is provided to this function, it operates on it and provides result.
Here, data is put into separate structures and there is no binding between data and functions.

In Object oriented world, a program or system is made up of collection of interacting objects. Every object has its own attributes and certain behaviour and responsibility.In OO way, when we have problem to solve, we think about objects that plays role in problem and the responsibility of each object, rather than thinking about the procedure to solve problem. The objects interacts with each other by sending messages (calling methods) to each other. Then We write classes that has data and behaviour binded together and the objects of classes interacts to solve the problem. So the entire way of thinking in OOP is different than procedural programming.

The concepts of OOP resembles to real world. In our world, everything is object with attributes and certain behaviour. e.g. consider a dog. It has attributes like color, size, height etc. and it has behaviour like walk, eat, bark etc. These attributes and behaviour are not separate, they are the part of a Dog itself. When we perform any action, a set of objects interacts with each other.

The main features of OOP like encapsulation, polymorphism and abstraction can be easily seen in the real world also. Consider a break of car. The break has certain attributes and behaviour. It is encapsulated within the assembly of break. The internal details of how the break operates is not visible to the user. User just preform the action of pressing the breaks and break perform the action of stopping the car. How break achieves this is abstracted from user. The mechanism break applies to stop the car is private to the break and external world, including user need not know about it i.e. the internal implementation is abstracted from user.

Lets get back to the original question, Is OOP difficult ? I guess, its not difficult and certainly different than procedural way and we need a entirely different way of thinking. Just by using a language like C++, Java or C# that supports OOP doesn't makes our code OO. OO way of thinking is must for witting good OO programs !