If this code has a clear goal or support for P technical support, you should usually reorganize it. Otherwise, you will have a classic problem with cloned code: in the end, you will find that you need to change the code that supports P, and you will not find all the clones that implement it.
Some people suggest that 3 or more instances be a threshold for refactoring. I believe that if you have two of them, you should do it; finding another clone [or even knowing that they can exist] in a large system is difficult, whether you have two or three or more.
This answer is now provided in the context of the absence of any clone search tools. If you can reliably find clones, then the original reason for refactoring (avoiding maintenance errors) is less persistent (the usefulness of having a named abstraction is still real). What you really want is a way to find and track clones; abstracting them is one way to ensure that you can “find” them (making the search trivial).
A tool that can reliably find clones, at least, can prevent you from avoiding error cloning service errors. One such tool (I am the author) is CloneDR . CloneDR finds clones using the langauge target structure as a guide, and thus finds clones regardless of layout spaces, changes to comments, renamed variables, etc. (It is implemented for a number of languages, including C, C ++, Java, C #, COBOL and PHP). CloneDR will find clones in large systems without giving any indication. Shown are the revealed clones, as well as an anti-malware, which is essentially an abstraction that you could write instead. Versions of it (for COBOL) are now integrated with Eclipse and show you when you edit inside the clone in the buffer, as well as where other clones are located, so you can check / review others while you are there. (One thing you can do is reorganize them :).
I used to think that cloning was simply a mistake, but people do it because they don’t know how the clone will differ from the original, and therefore the final abstraction is not clear at the moment the cloning action occurs. Now I believe that cloning is good if you can track the clones and try to reorganize after the abstraction becomes clear.
Ira Baxter Feb 19 '10 at 17:25 2010-02-19 17:25
source share