tags:

views:

80

answers:

3

Dear all,

We have a legacy system consists of class A, B and C. A is horrible and large. So is B. And C too!! (This is acknowledged within the company and we are trying to re-factoring them in the future).

The more scary thing is A and B extends C which they really shouldn't as they are not really the same thing. A developer (call him X) who did the coding admitted that he's done it this way so that A and B can use 'SOME' of the methods of C!

Now a client wants to update the legacy system and the change requires me to add the same change to A and B. What I can do is adding the change to C, so that A and B can see it. But if I do this I will be doing the same thing as X did.

What I have in mind is to write another class D, so A and B can have a D of their own and get the change. What do you think?

Thanks in advance!

Sarah

+4  A: 

Makes sense. Josh Bloch also recommends in effective Java the use of composition over inheritance. While you're at it check if you cannot move a couple of the other shared routines over too. Many small steps cover a large distance.

Peter Tillemans
If you can add some Unit tests before, it would be better, in order to be more confident in your refactoring...
Sylvain M
+1  A: 

It is hard to give a definite answer a question that is couched in such general terms.

The idea of moving common (stateless) methods into a helper class can be a good one. Similarly delegating calls on one class to calls on another class can be a good way to reduce the size of classes that have gotten bloated.

However, it is difficult to say whether helper classes or delegation are the best solution in this particular case. For example, if the methods on your existing classes are tightly coupled, it might be difficult to tease them apart into separate classes.

Maybe the best solution would be to bring forward the refactoring of the existing classes and get rid of the inappropriate (as you report) base class.

Stephen C
Yes, what's exactly what I would like to do - to bring forward the refactoring. However the reality is we have to push the changes out first... :(
sarahTheButterFly
@sarahTheButterFly Be careful with this attitude : make sure every change you make leaves the code cleaner than before you started. Do not release your code before it is cleaner. It does not have to be spotless, just less complicated. Remember the words: "If you find yourself in a hole, the first thing to do is stop digging."
Peter Tillemans
@Peter Yes, I understand what you mean. I totally agree with Stephen C's suggestion about bringing forward the refactoring process. However I am not the decision maker. I wish I was! I do try to leave the code cleander before I started. That is why I ask the question here.
sarahTheButterFly
A: 

Sarah,

It is hard to give concrete advice without knowing the exact relationship between A, B, and C. I'm going to assume A and B must be similar in some way if they require the exact same change. So, assuming A and B are similar but different from C, you may want to perform the following:

  1. Create a parent class, D, that both A and B extend from. This parent contains the functionality that A and B share (which you plan on changing).
  2. Either create a separate class that contains the functions A, B, and C share and have C and D have an instance of it OR create another parent class, E, for both C and D that contains the functions that A, B, and C have in common.

Again, my advice may not be valid if my assumptions are wrong. Would it be possible for you to give us a better idea of how A, B, and C are related?

Jake Greene
Thanks Jake for replying. Yes, I think there is a possibility that A and B can have a parent class D. However both classes need to be re-factored and broken down to a lot of small classes. I wish I could clearly outline what A,B,C are like and how they are related, but I just cannot as they are way too messy and complicated. We are thinking of re-writing them but I do remember Joel's article about Netscape's story... I think this post should be closed off, so that I won't confuse more people.
sarahTheButterFly
Not a problem, Sarah. I think we've all been in a similar position to the one you're in right now. You just have to make due with what you're given and hopefully make the system a bit better in the process.
Jake Greene