Object-Oriented Blog Process - III

It's 9:30am on a Saturday, and I'm up fully dressed, fully bathroomed (in lack of a better word) and writing a blog entry. For all of you who knows me through my blog, through #coldfusion on dalnet, through email or other cyberspace means of interactions this might not seem so significant. But if any of my work coalegues had seen me they would have been pretty close to calling 911 (actually the number here in Norway is 113). The reason for this is that I am an extreme nightowl, and at this time in the mornings on normal workdays I'm hardly awake despite of the alarm clock screaming in my ears, my wife pulling my hands and legs (that is if she is awake) and my two year old daughter jumping up and down on my head (my five year old is sleeping long, just like me). On a Saturday morning the alarm clock is shut of, my wife and five year daughter are asleep and my two year old is downstairs with the grandparents. So I would normally be passed out for a couple of hours more at the least.

Ok - what on earth does this have to do with OO you are probably asking yourself (if you are patient enough to still be reading). Well the reason I am awake and writing is this: I read the first half of the first chapter of "The Object-Oriented Thought Process" yesterday evening. And while I had no problems what so ever falling asleep when I went to bed (around 2am), my head was started working hard in the morning (I probably was slightly awaken by the two year getting out of her bed and running to grandparents).

In short, here's what got me thinking:

In Object-Oriented programing you have the data (properties) and functions (methods) all nicely gathered within the object, while in procedural programing the data and the functions working with the data are separated.

Now, I knew this from before, but somehow, based on yesterdays reading, my understanding sored to a whole new level this morning still half asleep in bed.

Consider the following situation:

You are running a webshop selling,....let's say... golf equipment (this one is for you Scott). And in this webshop one of the zillion items is a "Boyzoid Custom High-Speed Golf Ball". Now this golf ball is presented on it's own product detail page, in the golf ball category page, on the recomended product page, and on several other pages as well. On each of these pages you are using queries to the product db to get information like product name and price. So far so good.

But then you realise that the golfball need to belong to a tax group (in Norway it would be 25% VAT on a golf ball). You could of course just put the price included VAT directly on the ball. But then you realize that if the VAT would change to 26% when a new goverment is elected you have to update the price of a zillion products manually (you can not run a script adding the 1% because some products belong to other tax groups that might not get increased). So you deceide to stick a taxGroupID property on the products, and then keep the percentages of the diferent taxgroups separate in tax group table in the db. Now on all the pages our golf ball is presented we need to add a query to get the apropriate tax percentage so that we can calculate the price. It's getting a bit tedious.

Then your boss comes up with the idea that you should have different super-sale categories, so you need to ad a property on the golf ball (and all other products) what super-sale category they bellong to, and then a separate table for keeping the percentages of discount for each category. So now you need to add yet another query on the pages for our golf ball getting the propper discount (if any) for the super-sale category the ball belongs to. Are you sure you remembered to add it in all places? Now it's starting to get nightmare'ish.

And so it goes on and on. Welcome to spaghetti code maintenance hell.

The OO solution:

You make your golf ball a product type object (an instance of a product class - more about classes and instances later) with both the properties and the methods. So on all the pages you just call the getPrice() method on your golf ball object. Whenever there's a change you just edit the calculation within the getPrice() method. You don't have to worry about going into the different pages the golf ball is presented at all.

Welcome to OO code maintenance heaven.

Some final words:

The example above is just that; an example. There are many ways you could make life easier a procedural code with custom tags, and other code reuse methods. On the other side, the example above is also fairly simple. There could be several other factors that could come into play, like diferent prices for different groups of customers, different taxgroups depending on the country it is ordered from etc. And still a webshop deals with relatively few and simple kind of objects and calculations.

If there are someone who now don't see the advantage of OO programing please raise your hand (in other words leave a comment). Actually that goes for all of you - I love getting feedback of almost any kind.

Comments
The "issue" that I have with examples like this is that you still need to make the changes to your code somewhere, whether it be in the object you've created for the golf ball or the client-side code. Now, I'm an OO enthusiast and I would certainly use a GolfBall object to encapsulate the data, but my point is that I don't know if it's "maintenance heaven" if you still have to crack open code to make a change to the price. Based on my understanding of your example, you'd have an object for each item that you sell that encapsulates the data/methods for that particular item, but with generic names like "getPrice()" so that you can have a generic interface in your calling code. That *still* means though that if the VAT tax changes for 10 items, you have to update 10 classes rather than 10 product pages. I like the abstraction of the classes from the client code, but it's still 10 places to change the VAT tax, no?

Again, I've shifted to objects for all of my new work, as it just makes more sense to me. That being said, I guess this is a bit of a "devil's advocate" comment where, based on my interpretation of your example, you're not reducing the *number of changes* to your code (hence "maintenance heaven"), rather than *places* in which you need to change it.

Good stuff though. I *have* to get this book.
# Posted By Dave Carabetta | 10/29/05 6:44 PM
Hi Dave,

Thank you for the reply. I think you've misunderstood my example though. That's probably understandeable. Not only am I a real OO newbee my self. But I also want to use language that someone totally new to OO would understand. So I try to describe what I mean without any OO lingo not presented yet. But as an answer to your reply I'll try and do just that.

Let us say that we have a webshop products that are presented in 10 different page templates (product site, product category site, front page, speciall sale page etc.). Now with a procedural approach, if you needed to bring in something new, like taxCategory, to calculate the prices in each of these templates you will need to go into each of the ten templates to do the change. So far you understood my example right.

But in a OO approact all of the products bellonged to the product class, and all the ten pages would only call the getPrice() method of the instance of the object each product would be. Therefor you would only have to make the change one place, in the getPrice() method code in the product class. Another change to the calculation and you would again only change code once in one place - the getPrice() method in the product class. Now you would have done two changes in the OO approact, as oposed to twenty changes in the procedural approach.

And this is still in a system dealing with one class. Imagine a system with houndreds of different classes being used in houndreds of different page templates. That would add up to A LOT of saved work (not to mention reducing the possible coding mistakes with all that can entail).

I'm sorry for not explaining my thoughts clear enough in the first place, and hope I come through more clearly now.

ps - I did mention that the example was just an example to illustrate my thoughts. In a real world application (both OO and procedural) things would for sure look differently.
# Posted By Trond Ulseth | 10/29/05 7:51 PM
Hi Dave - me again,

Looking back I could see from my writing that it could be rather confusing - so I changed one sentence around a bit. I think it might make more sense now. I could have gone more in detail about classes and instances, but that was not the point of this post. I will for sure come back to this later though.
# Posted By Trond Ulseth | 10/29/05 8:49 PM