On Dot Matrix Harel Malka makes the case that there's a significant "inheritence tax" involved when you create a CFC that's derived from another (or in ColdFusion terms, when CFC2 extends CFC1).
With that in mind, he asserts that object composition is a better approach.
Is there a problem with inheritence? Is composition a better approach?
As in all things, the real answer isn't that simple. Or yes, no, and maybe.
Doing my own tests on the matter on a dedicated server, I found that with trusted cache turned on the difference in performance was about 5% per step, or more of a "sales tax" than an inheritence tax.
Given that, it really comes down to the question of whether or not inheritence makes sense for your application. For example, in the Datum ORM system, each business object is actually two objects, one a "hidden" object that contains all of the auto-generated "scaffolding", and the "visible" object which is inherited from that, and which lets the developer override any of the auto-generated functions.
While there's a small performance hit involved in this pattern, it's balanced out in two ways:
1) It's flexible, allowing the developer a great deal of control while still allowing the auto-generated code to be rebuilt and extended whenever there's a configuration file change.
2) It minimizes object creation and parameter passing.
While composition of objects may seem like a better arrangement, you in fact have to create that second object. Do the loop test and you'll see that while there may well be an "inheritence tax", creating and instantiating two objects takes twice as long as creating one. Who'd have thought?
Not only that, but if the first object is a facade for the second, you're going to spending a semi-significant amount of time calling functions and passing parameters and results between objects (which his empty object tests didn't account for), again eating into your theoretical performance gain.
In short, you're better off structuring your application in ways that make sense. Inheritence is a tool. Composition is a tool.
Choose your tools wisely.
Michael, (seems like we're comment swapping here ;o)
I think my point was missed here. I use extension all the time, in the same scenario you describe. It’s a good and useful tool as is composition. My test was regarding 3 levels extensions. Where you have an object that extends another, and that one extends yet another. I would have expected, in an ideal world, that the compiled object will not bring with it 3 object instantiation penalty with it, but it does. (I’ve added the 2 level extension to the test as an after-thought but my original one involved no extension vs 3 level one). Composition allows you cache objects in that scenario where extension does not.
Harel
Posted by: Harel Malka | April 09, 2007 at 08:37 AM