Monday, June 18, 2012

The Monday Post: Programmer's Mind II - "Is A" vs. "Has A"

I had this planned out in my head for weeks. See, I've taken to looking at some old code for games others have written and I actually got into a stonewalling argument when trying to get around to fixing it.



See, there are two things when programming a class: Things are either an "Is A" or "Has A". It's difference between Inheritance (Is A) verses Composition (Has A)

This would be Inheritance and Composition done right:

This is how it works: Jeff Sandefur "Is A" Human. A Human, however, "Has A" - lot of things. Such as Arms and Legs. Going further you'd do torso and such but you get the point. The black diamonds are Composition, the arrow points the the parent in Inheritance. Saying it out loud makes sense, in terms of which to choose.

So why in the hell did this come to be:
Let me get this straight. Player "Is A" sprite, but also has heath, damage dealing potential, and a host of other things I didn't bother putting into UML. Moreover, you are going to argue with me when I try to take out parts of Sprite out of Player into it's own sprite handler of sorts? I'm not kidding, someone else had to come up and take over talks on cleaning up the code after the argument stonewalled me for 10 or so minutes.

Just goes to show, semantics can be a bitch.

No comments: