Code duplication, also sometimes known as code cloning, is a programming practice consisting in repeating the same (or similar) sequences of code statements within a software same project. Disadvantages of c.d.: bug propagation, increased code complexity, code clutter. Advantages of c.d.: reduced development effort, increased reliability (sometimes). Automatic c.d. detection tools exist including, e.g., Simian, Dude, CCFinder, Clone DR.
Questions tagged [code-duplication]
711 questions
307
votes
21 answers
How do I remove code duplication between similar const and non-const member functions?
Let's say I have the following class X where I want to return access to an internal member:
class Z
{
// details
};
class X
{
std::vector vecZ;
public:
Z& Z(size_t index)
{
// massive amounts of code for validating…

Kevin
- 25,207
- 17
- 54
- 57
145
votes
11 answers
Is duplicated code more tolerable in unit tests?
I ruined several unit tests some time ago when I went through and refactored them to make them more DRY--the intent of each test was no longer clear. It seems there is a trade-off between tests' readability and maintainability. If I leave…

Daryl Spitzer
- 143,156
- 76
- 154
- 173
87
votes
13 answers
How to detect code duplication during development?
We have a fairly large code base, 400K LOC of C++, and code duplication is something of a problem. Are there any tools which can effectively detect duplicated blocks of code?
Ideally this would be something that developers could use during…

David Dibben
- 18,460
- 6
- 41
- 41
70
votes
3 answers
Suppress duplicate warnings in IntelliJ IDEA by annotation
Since version 15, IntelliJ warns me about code duplicates.
In some cases this might be intentional, so I want to ignore/suppress this warning by using the @SuppressWarnings annotation. But what is the correct value for this?
Edit: I'm not asking for…

Sebastian
- 5,721
- 3
- 43
- 69
53
votes
13 answers
How much duplicated code do you tolerate?
In a recent code review I spotted a few lines of duplicated logic in a class (less than 15 lines). When I suggested that the author refactor the code, he argued that the code is simpler to understand that way. After reading the code again, I have to…

Sylvain
- 19,099
- 23
- 96
- 145
49
votes
2 answers
How to reduce code duplication when dealing with recursive sum types
I am currently working on a simple interpreter for a programming language and I have a data type like this:
data Expr
= Variable String
| Number Int
| Add [Expr]
| Sub Expr Expr
And I have many functions that do simple things like:
--…

Scott
- 591
- 5
- 9
48
votes
2 answers
How can I use multiple constructors to remove duplicated code while maintaining readability?
int a, b, c;
Constructor()
{
a = 5;
b = 10;
c = 15;
//do stuff
}
Constructor(int x, int y)
{
a = x;
b = y;
c = 15;
//do stuff
}
Constructor(int x, int y, int z)
{
a = x;
b = y;
c = z;
//do stuff
}
To…

Cheese
- 1,429
- 3
- 16
- 17
37
votes
8 answers
Code duplication caused by primitive types: How to avoid insanity?
In one of my Java projects I am plagued by code repetition due to the way Java handles (not) primitives. After having to manually copy the same change to four different locations (int, long, float, double) again, for the third time, again and again…

thkala
- 84,049
- 23
- 157
- 201
35
votes
15 answers
How to convince a colleague that code duplication is bad?
A colleague of mine was implementing a new feature in a project we work on together and he did it by taking a file containing the implementation of a similar feature from the same project, creating a copy of it renaming all the global declarations…

vitaut
- 49,672
- 25
- 199
- 336
35
votes
3 answers
Is there a way to measure duplicate code?
I'm looking for a code duplication tool that is language agnostic. It's easy to find language specific code duplication tools (for Java, C, PHP, ...), but I'd like to run some code duplication analysis on a templates in a custom syntax.
I don't care…

Stefaan
- 4,696
- 3
- 23
- 16
26
votes
4 answers
Do tools exist which automatically find copy-and-paste code?
Are there tools out there which could automatically find copy-and-paste code among a set of files?
I was thinking of writing a script for this, which would just search for equal strings, but such script would find mostly irrelevant equalities. (Such…

java.is.for.desktop
- 10,748
- 12
- 69
- 103
24
votes
3 answers
Avoid code duplication when using C++11 copy & move
C++11 "move" is a nice feature, but I found it difficult to avoid code duplication (we all hate this) when used with "copy" at the same time. The following code is my implementation of a simple circular queue (incomplete), the two push() methods are…

user416983
- 974
- 3
- 18
- 28
22
votes
8 answers
Java multi-type method parameter?
I wonder if it is possible to require that a java method parameter is of any type from finite set of types. For example - I am using a library where two (or more) types have common methods, but their lowest common ancestor in the type hierarchy is…

egelev
- 1,175
- 2
- 11
- 27
20
votes
20 answers
Any valid reason for code duplication?
I'm currently reviewing a very old C++ project and see lots of code duplication there.
For example, there is a class with 5 MFC message handlers each holding 10 identical lines of code. Or there is a 5-line snippet for a very specific string…

sharptooth
- 167,383
- 100
- 513
- 979
19
votes
9 answers
Keeping track of utility classes
I've recently been more and more frustrated with a problem I see emerging in my projects code-base.
I'm working on a large scale java project that has >1M lines of code. The interfaces and class structure are designed very well and the engineers…

Asaf
- 6,384
- 1
- 23
- 44