Wednesday, 4 February 2015

Object Oriented Programming Overview

Object Oriented Programming is a different approach at programming where the application or code is made up of many parts (objects) and its built piece by piece to create a functional program. It uses the concept of treating parts of the code as "objects" in which these objects contain certain "attributes" and when completely put together can make up the functional program allowing for a logical and simple structure, flexibility, and readability. Object Oriented programming is more read-friendly to humans and has multiple support inside the coding language to create it.

Learning about object-oriented programming has really changed the way I code and likely the way many people code when it was first introduced. The most striking difference is the mindset "adopted" when coding in OO as compared to procedural programming. The most prominent difference is in PHP from my experience, where one learns coding it from a procedural point of view and then learns the OO approach. A example in pseudo PHP code, extends means A extends B, A is a child of B. $this is a the keyword for the python equivalent self and does not need to be passed in the function argument as PHP already does this automatically:

As you can see, with the procedural approach, we made it a bit more intuitive than normal. We created function that preform specific tasks such as eat an orange, or peel a banana. We are under the assumption that peeling an orange and a banana require two difference types of actions and thus require different code for both of the, but eating them is universal. We started by creating a value for an orange and a banana, then peeled them and got a new value for them and assigned that to them, then we ate them and assigned the new value to the orange and the banana after they had been eaten. Essentially we were creating a value, and creating a new modified value and assigning it to said previous reference and did this a bit like issuing commands to a command line and getting the wanted final result. It is logical to read from up to down and you can follow it, but it's not very intuitive or easy to read when you get to more complex code. Let's take a look at the OO approach







The OO approach does things differently. We can see that the only thing that seperates an orange and a banana (other than the real world physical difference) is the way they are peeled. Otherwise, they all share common characteristics, the eat method and the peel method. They are also both FRUITS, and this is where OO becomes to be relevant. Since both the orange and the apple need to be eaten, and since there is no difference in code between eating an orange and eating a banana, we create the super class "Fruit", a class that will not only show the similarities of an orange and a banana, but also act as a general class for ANY "fruit" we create, whether it be a grape, or a pineapple. We can't implement a peel method in fruit (although we could do something like declare that all child fruits (oranges, bananas) must have a eat method in them, this can be done through abstract classes and I have shown below what it might look like along with improvements to the OO code:






Now this code shows completely what we want to achieve through OO. Before, we were doing almost the same thing as procedural, just changing the functions under this fancy "class". But now we are treating our code as objects and directly manipulating them instead of returning some value. We know that every fruit has to have some "peel" function, so we declared it as an abstract function which pretty much means, I don't know how to peel said fruit, but I know that if you are going to call yourself a fruit, you should be able to peel it, enforcing that every fruit that is a fruit (child of class Fruit), must have a function that peels it. 

Now, our peel function is immediate and directly manipulates the fruit object, instead of returning a value like it used to. If we are eating the fruit it should be something like this:

$orange = 'not eaten';
$orange->eat_fruit();
print($orange);
'Eaten'; //Although this would be a JSON call

As you can see, we are treating our code as objects, directly manipulating it, taking advantage that it has many shared features of other parts of our application and combining them into one universal category to allow us to give them special attributes to them. Oranges, Apples, Grapes are all fruits and all need to be eaten the same way, but not all peeled the same way, some of them (Grapes) don't need to be peeled while some (pineapples) have a radically different way of being prepared/peeled. So the object hierarchy is something like:

Fruits -> (Oranges, Bananas, Grapes, Pineapples)
And maybe more intuitively:
Fruits -> (Oranges, Bananas, Grapes, Berries -> (Blueberries, Blackberries, Raspberries), Pineapples)

All these share common attributes (functions or methods as seen in the code) but almost all have something specific to them. A few examples could be if we introduced a variable or methods called color or calories, sugar_content, create_juice. Not many people create juice purely from bananas, but a create_juice method for oranges makes much more sense. The power in OO is creating this logical and easy to understand structure while maintaining machine logic and flexibility to control the code more. When you see the procedural code, you may or may not know what an orange can do, what it can't or what it needs to do (especially since in code, variable terms aren't nearly as simple as an orange or an apple, but in object oriented code, you know what it cant do or can do, and you also know what role it plays in the code. Like a house:

House -> (Kitchen -> (Sink, Stove, Cabinet -> (Forks, Knives), Fridge -> (Lettuce, Tomatoes)), Living_Room -> (TV -> (Screen, Remote Control), Couch, Lights -> (Methods: Turn On, Turn Off, Replace)), Garage -> (Garage Door (Methods: Open, Close))

Forum -> (Topic -> Array of Comments -> (each comment as variables and methods: comment_text, author_username, insert_reply, delete))

When correctly utilized OO is a logical and intuitive way to approach a large problem and break it down into smaller pieces and solve them to create a functional solution, something that would just be too complicated to do in procedural as the problem scales up. After all, it is a progammer's job to take a impossible large task and divide it into possible smaller tasks. 




No comments:

Post a Comment