Geeks With Blogs

News

Series

Add to Technorati Favorites


An Archived Managed World This blog has moved to http://www.managed-world.com/blog

I'm not sure if this is going to be any kind of series at all, but I thought it was worthwhile to label this with “#1” just in case I have future discussions on a similar topic. If, as a human being, you are repelled by the mere thought of continuity, then you may politely turn around, and excuse yourself from this and any future discussions of the matter. Continuing, as I was rummaging through family letters that have been around for many hundreds of years (all preserved in display cases filled with Argon in order to preserve their wonderful revelations), I came across the following letter that seems to have been a speech written by my great, great, great, great, great, cousin-in-law, thrice removed. I know that some of the words and descriptions seem fairly “recent“ or “progressive“, so I too was surprised to see them used and found it quite shocking that the letter still carries the weight that it does today. It seems like all of us could learn a lesson or two from this letter, so I felt that I should post it here, in its entirety, with no changes made for easier comprehension (the unabridged, unadulterated edition if you will):


My Dear Fellows,

Before answering the said question posed to me by Sir Orientation, Duke of Objectshire, in a prior correspondence, I would like to take this opportunity to thank the board of Ineta for this wonderful space to house these debates, and would also like to thank all the loyal fans and readers that have made this possible. Without you, there would, in no doubt, be a sore lacking of truly gentlemanly fellows in this country. 

With this discourse now well established, I would like to discuss a travesty that has been brought to my attention in the most recent of times. The travesty that I hereby discuss pertains to the severe treatment of a libelous and nefarious nature towards our wonderful and fair maiden, Lady Inheritance. Some have come to believe that Lady Inheritance is someone who deserves to be used often, or as some may refer to her, a “slut”. I decree that this slander shall not go unpunished. How long shall it be 'til we awake from this hazy slumber to realize that Lady Inheritance is someone who shall be treated with profoundly deep respect.

No more shall we use her as a mere tool when left to our own devices. Nay, she deserves more. No more shall we establish our own bastardized, immoral, trees of inheritance produced by the fruits of our selfish loins. Nay, she deserves more. No more shall our ill-fitting logic go unchecked while engaging in discourse with our fair lady. Nay, she deserves more.

I plead with you, my dear fellows, that you protect our fair lady. Ensure that no advantage is taken of her. Abuse of our fair lady shall be no longer. I urge you to stand up and protect Lady Inheritance. This is not a mere decision that you can continue to choose to ignore. Nay! This is of utmost moral importance. For the sake of our livelihood, we need to protect our fair lady and ensure that she is kept most chaste and upheld in the most pure view in which she deeply deserves.

Thank you all, I do,

Sir Charles of Compushire


Now! For any of you out there who do not follow this said letter, I will now attempt to break it down and better illustrate exactly what my great, great, great, great, great, cousin-in-law, thriced removed was explaining, for it is an issue that I strongly believe is still a problem today.

The problem that exists is that we developers, especially developers new to the concept of object-oriented programming, tend to abuse the concept of inheritance. It is quite easy to tell the new OOP developers because every class is derived from something, and there is often no “composition“ present. Even the inheritance that does exist usually does not make much sense. The trick is the distinction between proper and improper use of inheritance can be quite a fine line. The best way I can think of to explain a typical misuse of inheritance is by an example.

Since I've kind of been hooked on game development in the most recent of times as of late, I shall, I mean, I will use a typical game you might find. Let's say we have a 2d game we are developing, and just for the heck of it, we will call it “Tario“ and it will feature a hero roofer that is going to save his King from an evil, fictional monster. The first game class we might develop is a “Sprite“. This will be an abstracted class that will let us draw an image on the screen. Common Logic Flaw - Tario will need to be drawn on the screen, so we will derive our Tario class from Sprite. This is a mistake I see often (and it doesn't just apply to game developers).

A question for you: Does it truly make sense to derive Tario from Sprite? In my opinion, it does not. A sprite is merely one possible physical representation of our player Tario. Tario is *not* a sprite. Tario is visualized in our game *by* a sprite. This may seem like a small nit-picky difference, but it can have a potentially large impact on your possible domain model. You have to remember here that there are multiple tools in your toolbox and inheritance is merely one of them. A more fitting tool in this situation, I believe, is composition. Don't derive Tario from Sprite, have Tario contain a Sprite instance that he uses to represent his physical self.

[NOTE: The solution above still isn't perhaps the best design yet, but I think it serves its purpose well as an example (a point-counterpoint, if you will :))]

I think one of the reasons you find beginners doing this is because the importance of inheritance is drilled into them as one of the tenets of OOP. They learn about “is a“ and “like a“ relationships, but don't really learn about “has a“ relationships (at least, not with the same importance). OOP is not a topic to really be taken likely. The fact that there are often multiple ways to skin the cat only makes design in OOP environment more difficult. Object-oriented design a skill. It is a skill that needs to be honed, often over multiple years of use. Once you do have more than a mere apprentice's grasp on OOP, then you get to learn the really fun stuff like patterns, and similar concepts of the same ilk.

This is one of the reasons that I often like to think about object-oriented design as role/responsibility analysis. I believe that thinking about roles and responsibilities will ultimately lead to a better design than just thinking about “is-a“ and “like-a“ relationships. I know there are multiple paths leading to the same “nirvana“ of object-oriented design. Role/Responsibility analysis is simply the way that I have found that works best for me. No matter what technique you use, as long as you come up with a properly designed, well thought out and easily understood model, I think the path is perhaps irrelevant. With the more experience I gain with object-oriented design, I find myself using inheritance more to establish polymorphic behavior than to establish relationships. I pose this question to you: Is there any value to a class design that uses much inheritance, but never once illustrates polymorphic behavior?

So, as a gentle reminder, please remember that you have more than just a hammer in your toolbox. There are other tools that will solve your problem better than inheritance. Inheritance is but one option.

Thanks!

 

Posted on Monday, October 25, 2004 7:20 PM Game Development | Back to top


Comments on this post: OOP Abuse #1 - Inheritance (a.k.a. "My Dear Fellows")

# re: OOP Abuse #1 - Inheritance (a.k.a. "My Dear Fellows")
Requesting Gravatar...
I would like to congratulate and thank you for posting this very subtle, often misunderstood, and potentially abused or misused concept. The
"is-a", "like-a", and "has-a" analogies definitely
need to be preached out loud.
Left by Fraggin_Target on Jun 29, 2005 12:06 AM

# re: OOP Abuse #1 - Inheritance (a.k.a. "My Dear Fellows")
Requesting Gravatar...
Very entertaining read, dear Sir. I commend you. Sir Orientation, Duke of Objectshire, indeed.
Left by Actionscribe on Aug 18, 2008 11:38 PM

Your comment:
 (will show your gravatar)


Copyright © Jason Olson | Powered by: GeeksWithBlogs.net