Monday, June 23, 2008

Beginner: Understanding OOP

Okay. Let talk about programming in general. First of all let's differentiate between two types: procedural and object-oriented.

What is Procedural Programming?
Procedural programming is the type of programming is the common and most intuitive method of programming. The code of a program created using this method is written linearly and is dependent on the order in which each statement of code is written. This is not to say that object-oriented programming is not also procedural at some level, what we need to identify is that the problem is solved using procedural logic. This sort of logic is similar to how people thing.

For example, imagine a clerk at a donut shop:
  1. First, mix ingredients for dough.
  2. Mold it into a ring.
  3. Bake it golden brown.
  4. Coat it with chocolate.
  5. Sprinkle sugar.
Each of these steps must be performed in this exact order and changing any of these steps may change the entire program. If you had asked the clerk to make a bagel instead the clerk might be smart enough to see that most of the steps are the same and say substitute the donut dough for bagel dough and add onions and garlic as toppings. But if you asked the clerk to make a pretzel instead so many steps would have to be rewritten that you might as well not use the same instructions at all.

Modification of the program also requires a person to first understand all of the steps involved before making any changes to the procedure since often the state of different variables may change during the course of the procedure.

What is Object-Oriented Programming?
So then what now is object-oriented programming, or OOP? OOP is a method of breaking down problems into individual blocks of logic. The idea is that the sum of these blocks solves the problem in an elegant yet easy to understand manner.

When we change the logic in the program, of course depending on what is changed, we are also able to be more flexible in our changes. Since the code is divided up into blocks we can change certain blocks without affecting the rest of the code. Also, understanding the big picture of what each of the blocks do allows us to keep track of fewer details in the code and keep the program in general more organized.

Furthermore, the fact that we have separate blocks of logic allows us to create a separation of logic that allows multiple people to work at the same time. As long as each person understands what to expect from each other, they can easily write code that can communicate. Smaller problems being solved at the same time means that, in most cases, we are able to solve the whole problem in shorter time, this may be debated of course by many since there is the overhead of assigning tasks and dividing the problem.

Our same donut shop clerk program might be separated in to groups of tasks.
Dough Tasks:
  1. First, mix ingredients for dough.
  2. Mold it into a ring.
Baking Tasks:
  1. Bake it golden brown.
Topping and Seasonings Tasks:
  1. Coat it with chocolate.
  2. Sprinkle sugar.
Main Donut procedure:
  1. Dough Tasks
  2. Baking Tasks
  3. Topping and Seasonings Tasks
This way we could have two programmers, one to manage the dough tasks and another to manage the seasoning tasks.

Solving Problems Using Design Patterns
Object-oriented programming's main advantage is being able to solve problems through architecture. This is a tremendous advantage because we can solve problems based on different "arrangements" of code. Let's think about this more.

On the contrary, in procedural programming remember a programmer must first understand all of the code before changes are made, rely heavily on conditional logic and reuse code by copy-and-paste.

Most programming problems are common reoccurring issues. The hassle is solving these same problems over and over again when they have very small differences. OOP allows us to make common solutions. These common solutions are called design patterns and are generally architectural solutions. The challenge of OOP does not become memorizing procedures and branches but now gracefully applying design patterns and integrating them into your logic.