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
11
votes
9 answers
Eliminating code duplication in a single file
Sadly, a project that I have been working on lately has a large amount of copy-and-paste code, even within single files. Are there any tools or techniques that can detect duplication or near-duplication within a single file? I have Beyond Compare…

Jon Onstott
- 13,499
- 16
- 80
- 133
10
votes
5 answers
How to detect the amount of almost-repetition in a text file?
I am a programming teacher, and I would like to write a script that detects the amount of repetition in a C/C++/Python file. I guess I can treat any file as pure text.
The script's output would be the number of similar sequences that repeat.…

nowox
- 25,978
- 39
- 143
- 293
10
votes
1 answer
Using __class__ to create instances
Is it a good idea to use __class__ to create new instances within the class?
The following code is an example of doing this:
from collections import namedtuple
_Position = namedtuple('Position', ['x', 'y'])
class Position(_Position):
def…

Joschua
- 5,816
- 5
- 33
- 44
9
votes
3 answers
Duplicate code detection: Tools you can use
I am looking out for a software that identifies duplicate/redundant Javascript code. I found one such tool named CloneDR, but don't know how good it is .
I was looking out for similar open source tools . Please guide .

Aazim
- 129
- 1
- 6
9
votes
2 answers
How to avoid duplication of code regarding primitive types?
Background
A bit input stream is backed by an array of bytes. There are a handful of methods that read from that byte array into various coerced primitive arrays.
Problem
There is duplicated code. Java lacks generics on primitive types, so perhaps…

Dave Jarvis
- 30,436
- 41
- 178
- 315
9
votes
3 answers
any ideas for avoiding duplicate code in C# and javascript
i have an asp.net mvc website where i build up most of the page using C# for example building up html tables given a set of data from my viewmodel
i also have a lot of javascript that then dynamcially modifies these tables (add row for example).
the…

leora
- 188,729
- 360
- 878
- 1,366
9
votes
6 answers
I need a tool to find duplicates or similar blocks of text in a singular text file or set of text files
I want to automate moving duplicate or similar C code into functions.
This must work under Linux.

vitaly.v.ch
- 2,485
- 4
- 26
- 36
9
votes
4 answers
How could one refactor code involved in nested usings?
I've got some code that has an awful lot of duplication. The problem comes from the fact that I'm dealing with nested IDisposable types. Today I have something that looks like:
public void UpdateFromXml(Guid innerId, XDocument someXml)
{
using…

ckittel
- 6,478
- 3
- 41
- 71
8
votes
3 answers
Remove code duplication for virtual class members in C++ inheritance
I have got myself into a strange issue now. Ill write a really simplified version of the same.
class Base
{
public:
virtual int func1()=0;
virtual int func2()=0;
protected:
int n;
};
class der1: public Base
{
// implements the virtual…

lifeOfPi
- 178
- 1
- 12
8
votes
1 answer
Detect duplicate code in Visual Studio 2010
Clone Detective was a great tool for finding duplicate code in VS 2008.
Are there any tools for finding duplicate code which integrate into VS 2010?
*Clone Detective doesn't look like its being actively developed 1 2.

Michael La Voie
- 27,772
- 14
- 72
- 92
8
votes
2 answers
Can SonarQube code duplication detector be parameterized to stop on method boundaries?
I’m using SonarQube on my Java projects, and want to eliminate code duplications from our code as far as possible.
My problem is that SonarQube’s code duplication detection doesn’t take into account method boundaries. It lists identical parts of…

f.r
- 83
- 1
- 5
8
votes
0 answers
Ignore duplicate import statements in sonar
I have a Groovy project that's being analyzed by Sonar 3.0.1. We have duplicate code detection turned on, using the Sonar detector.
In one set of files, it's detecting 11 lines of duplicated import statements. In another set of files, it's…

Mike
- 7,994
- 5
- 35
- 44
7
votes
3 answers
Duplicate code: How to find and remove with tools
I am managing a group of three interns working on a php project. They seem to be not good at refactoring and are using duplicate code in multiple places. I am looking for a tool which I can use to find this duplicate code so I can show them.
This…

Abhishek
- 1,749
- 9
- 27
- 39
7
votes
11 answers
How to avoid duplicate logic with Mocks
I have the following challenge, and I haven't found a good answer. I am using a Mocking framework (JMock in this case) to allow unit tests to be isolated from database code. I'm mocking the access to the classes that involve the database logic, and…

Yishai
- 90,445
- 31
- 189
- 263
7
votes
3 answers
in C++, How can I give a class copy constructor and assignment operator the same functionality without making duplicate code
Possible Duplicates:
What is the copy-and-swap idiom?
Copy constructor and = operator overload in C++: is a common function possible?
Is there a way that I can make the body of the copy constructor and assignment operator contain the same code…

Matt Munson
- 2,903
- 5
- 33
- 52