10

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)
  • ...
2

3 Answers 3

8

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.

6

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).

2
  • 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
    Commented Sep 11, 2011 at 21:06
  • The proper name from Refactoring book is "pull up". Commented Sep 11, 2011 at 21:09
2

I guess this is an open ended question but it also depends on the state of the code as well. I mean you can tolerate the duplicate code a little bit depending on the context. Rule of three is good for this matter.

Rule Of Three The first time you do something, you just do it. The second time you do something similar, you wince at the duplication, but you do the duplicate thing anyway. The third time you do something similar, you refactor.

Although this is pretty much arguable, this post also considers cases where you would tolerate duplicate code.

2
  • 1
    +1 about the "rule of three". I'm always amazed at how broadly applicable it is.
    – andy mango
    Commented Apr 15, 2017 at 1:26
  • 1
    This does not answer the how question.
    – Jan Doggen
    Commented Apr 15, 2017 at 14:32

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