Monday, October 25

Delegate Now

I'm working on a large code base, and I'm really starting to dislike inheritance as a way to combat code duplication.

Every time I see an "extends" keyword, I think, gee, this would be much nicer if it was using delegation. It would be more explicit. It would be easier to test. It would be easier to re-use the delegated code elsewhere.

But why dont we all use delegation all the time if it is so much nicer?

One theory is that creating new classes is too costly. While this might not be true, I suspect that we could easily feel this way: "What do I call the new class?" "Where do I put it?" "Do I have to test it?". All these questions probably add up to make it less attractive than "Extract Superclass".

But don't be fooled. Delegate Now, not later. Call the class "FooService" if you cant think of a name immediatley. Put it in the current package. Move or implement the test methods later.

Furthur reading:


Post a Comment

Links to this post:

Create a Link

<< Home