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
17
votes
5 answers
How do I put some code into multiple namespaces without duplicating this code?
Assume I have the method defined in the two different namespaces:
namespace foo
{
void print()
{
//do work...
}
}
namespace bar
{
void print()
{
//do work...
}
}
The foo::print() and the bar::print()…

yakov
- 444
- 3
- 14
17
votes
4 answers
Avoid Redundant @patch When Mocking with Python
Coming from a static programming language background, I am wondering how to best do mocking in Python. I am accustomed to dependency injection. Within tests, mocks are created and passed to the System Under Test (SUT). However, looking at Mock and…

Travis Parks
- 8,435
- 12
- 52
- 85
15
votes
1 answer
How to avoid code duplication of different structs with semantically equal fields/properties?
Given these two structs:
pub struct RectangleRenderer {
canvas: Canvas,
origin: Point,
shape: Rectangle,
}
pub struct CircleRenderer {
canvas: Canvas,
center: Point,
shape: Circle,
}
As I come from Java, I would extract a…

xetra11
- 7,671
- 14
- 84
- 159
15
votes
4 answers
Identifying repeated code within PHP project
I have a single PHP file within a legacy project that is at least a few thousand lines long. It is predominantly separated up into a number of different conditional blocks by a switch statement with about 10 cases. Within each case there is what…

robjmills
- 18,438
- 15
- 77
- 121
14
votes
6 answers
Best way to avoid duplicate code if two classes extending different class
I am working on an Android project and i am facing this situation.
I have 2 class :
class A extends B
{
openDoor(){
//impl
}
closeDoor(){
//impl
}
}
class X extends Y{
openDoor(){
//impl
}
closeDoor(){
//impl
}
}
Now if you observe the are…

user3114009
- 261
- 2
- 4
- 14
14
votes
6 answers
How can I find copy/paste (duplicate, clone) code in Perl?
I've searched the Internet for a while now and I have not been able to find any free (or cheap) tools/utilities/modules that can analyze a set of Perl files (modules or scripts) and flag duplicate or cloned or copy/pasted code.
I'm better now, but I…

Kurt W. Leucht
- 4,725
- 8
- 33
- 45
13
votes
5 answers
Tortoise SVN: compare two directories (instead of files or revisions)
You can compare two revisions or files with Tortoise SVN. Is there also a way to compare two directories?
I have two directories containing source code. 90 percent of the code is identical. Now I'm going to store identical code in a plugin since…

fishbone
- 3,140
- 2
- 37
- 50
13
votes
1 answer
Is there way in docker compose to create the cross service constant?
I have compose file like this:
service1:
//some stuff
environment:
- "PROP=some_common_value"
service2:
//some stuff
environment:
- "PROP=some_common_value"
service2:
//some stuff
environment:
- "PROP=some_common_value"
I want…

gstackoverflow
- 36,709
- 117
- 359
- 710
13
votes
9 answers
Any tools to check for duplicate VB.NET code?
I wish to get a quick feeling for how much “copy and paste” coding we have, there are many tools for C# / Java to check for this type of thing. Are there any such tools that work well with VB.NET?
(I have seen what looks like lots of repeated code,…

Ian Ringrose
- 51,220
- 55
- 213
- 317
12
votes
4 answers
Trade off between code duplication and performance
Python, being the dynamic language that it is, offer multiple ways to implement the same feature. These options may vary in readability, maintainability and performance. Even though the usual scripts that I write in Python are of a disposable…

Renae Lider
- 1,004
- 7
- 20
12
votes
15 answers
Is it better to have code duplication and have it be very simple/readable, or have no duplication (using generics) but be much more complicated?
In general I come across this a lot. Some of my co-workers prefer very simple, easy to read classes even if that means that there is some code duplication, whereas I do everything in my power to avoid code duplication, even if it means making more a…

darrickc
- 1,872
- 6
- 27
- 38
11
votes
3 answers
Visual Studio 2017 - How do I duplicate the current line?
I want to duplicate the current line to a new line below it. I found this post but I can't find the option they're talking about (editor.action.copyLinesDownAction) in options > keyboard.
How do we do this in 2017?

J.Doe
- 713
- 1
- 6
- 19
11
votes
4 answers
code duplication in javascript
Is there any tool to detect code duplication in JavaScript? I tried "PMD Duplicate code detector" but it is not supporting .js extension.

chandra
- 119
- 1
- 3
11
votes
2 answers
Kotlin: How can I avoid code duplication in constructors?
Often I find myself in a situation where I have a superclass that has lots of optional parameters, and those same parameters need to also be optional parameters in its subclasses.
For example, the superclass:
abstract class Plugin(val name: String,…

Jire
- 9,680
- 14
- 52
- 87
11
votes
4 answers
Avoiding code duplication in F#
I have two snippets of code that tries to convert a float list to a Vector3 or Vector2 list. The idea is to take 2/3 elements at a time from the list and combine them as a vector. The end result is a sequence of vectors.
let rec vec3Seq…

Johan
- 660
- 1
- 6
- 13