Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In an OO language (e.g. but not limited to Java) how do you fix duplicate code depending on the scope of it's occurrence? I would start with (for example)

  • in the same class (scope) perform the Extract Method refactoring (fix)
  • in classes of the same hierarchy (scope) perform Extract Method and Pull Up (fix)
  • ...
share|improve this question
    
have a look at an example for implementing DRY principle (Don't repeat yourself) with the same class at this: geekswithblogs.net/chrisfalter/archive/2008/03/07/… –  Emmad Kareem Sep 11 '11 at 21:31
    
Original question at SO (stackoverflow.com/questions/7380946/…) got closed. So I moved it here. –  Peter Kofler Sep 12 '11 at 10:47

2 Answers 2

In general - consolidate duplicate code into a single place and ensure original duplication site is calling the consolidated place.

In your examples, within a class this would be the extracted method and within a set of classes the pulled-up method within the base class.

In copy-paste code, this would be to remove the duplicates and ensuring that any users now use the single copy (in whatever level this is).

When dealing with different versions of libraries, consolidate on a single version (if possible).

share|improve this answer
    
Wouldn't it be the "pulled-down" method if it's in a base class? I always imagine base classes as physically under the derived classes. –  Dave Nay Sep 11 '11 at 21:06
    
The proper name from Refactoring book is "pull up". –  Peter Kofler Sep 11 '11 at 21:09
up vote 3 down vote accepted

Recently I found a good answer to my question in Uncle Bob's "Clean Code", which I want to share. He differentiates three types of duplication

Pieces of identical code should be replaced with a single method. So the fix would be to extract the method and delegate to common behaviour.

  • in the same method, perform Extract Local Variable and reuse it.
  • in the same class perform the Extract Method refactoring.
  • in classes of the same hierarchy Extract Method and Pull it Up. A hierarchy might be created to find a place for the methods.
  • in classes of separate hierarchies use delegation to new objects.
  • If the methods do not need any enclosing state, then the "lib" pattern might be applied (that is a container for static methods, usually called SthUtil or SthLib).

cases of switch/case and if/else that always test for the same set of conditions.

  • These should be replaced with polymorphism.

Modules that implement similar algorithms. These are hardest to find, as no clone detector can find them.

  • As the scope is larger design patters are used. Template Method design pattern might be applied for algorithms inside a class hierarchy.
  • Strategy design pattern might be applied for any algorithm that is used in different places.

Also a valid point mentioned by Oded, when dealing with different versions of libraries

  • consolidate on a single version. Facade design pattern might help here.

In the end the best one sentence to answer my question is by stimms:

code reuse method used in OO languages is objects.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.