Questions tagged [code-duplication]

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.

711 questions
7
votes
5 answers

To DRY or not to DRY? On avoiding code duplication and retaining cohesion

I've got a question concerning code duplication and refactoring, hope it's not too general. Say you've got a rather small piece of code (~5 lines) which is a sequence of function invocations that is - not a very low level. This code is repeated in…
lukem00
  • 859
  • 8
  • 17
7
votes
1 answer

How can you have two URL routes mapped to the same handler method in Spring MVC (3.0)?

I have a a userPanel method mapped to the /user/panel URL route: @RequestMapping(value = "/user/panel", method = RequestMethod.GET) public final String userPanel(HttpServletRequest request, ModelMap model) However, I would also like the userPanel…
7
votes
4 answers

template parameters, #define and code duplication

I have a lot of code like this: #define WITH_FEATURE_X struct A { #ifdef WITH_FEATURE_X // ... declare some variables Y #endif void f (); }; void A::f () { // ... do something #ifdef WITH_FEATURE_X // ... do something and use Y #else //…
Thomas
  • 1,001
  • 1
  • 10
  • 20
7
votes
3 answers

Is there a way to connect values (like enums) from frontend (JS) to backend (PHP/Ruby) without duplication?

Given a web application based PHP or ruby in its backend, and programmed on JavaScript in the frontend, sometimes we need to define values in both sides to process data that communicates between the frontend and backend through APIs. In JavaScript…
danikaze
  • 1,550
  • 2
  • 16
  • 34
7
votes
2 answers

Automatic code deduplication of assembly language?

I've been going through some Assembly Programming Videos to get a better understanding of how to manually optimize the *.s files left after compiling with gcc/g++ -S ... One of the topics covered was Refactoring Redundant Code that demonstrates how…
technosaurus
  • 7,676
  • 1
  • 30
  • 52
7
votes
3 answers

How to avoid duplicate code when operating on DOM in "up" and "down" direction?

I'm writing a JS webapp client. User can edit list/tree of text items (say, a todo list or notes). I manipulate DOM with jQuery a lot. User can navigate the list up and down using keyboard (similar to J/K keys in GMail), and perform several other…
Jakub P.
  • 5,416
  • 2
  • 21
  • 21
6
votes
2 answers

Is there a way to extract outer loops of multiple similar functions?

Example: I want to extract the nested for loops from these operator functions that are the same except the one line. // Add two matrices Matrix& operator+=(const Matrix& other) { for (int i = 0; i < this->m_rows; i++) { for (int j =…
JamesM
  • 73
  • 4
6
votes
9 answers

How to refactor rapidly evolving code?

I have some research code that's a real rat's nest, with code duplication everywhere, and clearly needs to be refactored. However, the code base is evolving as I come up with new variations on the theme and fit them into the codebase. The reason…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
6
votes
1 answer

Best practice for avoiding code duplication while implementing iterator and const_iterator classes

What are the best practices to avoid code duplication when implementing class pairs such as iterator and const_iterator or similar? Does one usually implement iterator in terms of const_iterator using lots of const_casts? Does one employ some sort…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
6
votes
1 answer

Can I use decltype() to avoid code duplication in explicit template instantiations?

I have a long template function declaration: template void foo(lots ofargs, goin here, andeven more, ofthese arguments, they just, dont stop); with no overloads. and I want to explicitly instantiate it. I can write (say for T =…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
6
votes
6 answers

Throw extra exception to avoid code duplication

First of all, I know the standard answer will be that exceptions are never to be used for flow control. While I perfectly agree with this, I've been thinking a long time about something I sometimes did, which I'll describe with the following…
MarioDS
  • 12,895
  • 15
  • 65
  • 121
5
votes
3 answers

How do I avoid code duplication when modelling a table, its layout, and its records, all of which share the same basic structure?

This will be a somewhat abstract question. I am working on a Data Access Layer framework which needs to distinguish between a table, its abstract schema/layout, and concrete table records. I'm afraid that because of this distinction, there will be…
5
votes
6 answers

How to avoid code duplication?

Is it possible to avoid code duplication in such cases? (Java code) void f() { int r; boolean condition = true; while(condition) { // some code here (1) r = check(); if(r == 0) break ; …
semekh
  • 3,867
  • 2
  • 24
  • 34
5
votes
0 answers

How to detect code duplication among multiple python source files?

I have placed all python project sources in a single folder. Running the following pylint only seems to be looking and analyzing for duplicates within each source file and not across all which is what I intended: pylint --disable=all…
SkyWalker
  • 13,729
  • 18
  • 91
  • 187
5
votes
4 answers

Deduplicate this java code duplication

I have about 10+ classes, and each one has a LUMP_INDEX and SIZE static constant. I want an array of each of these classes, where the size of the array is calculated using those two constants. At the moment i have a function for each class to create…
terryhau
  • 549
  • 2
  • 9
  • 18