duplicate code duplicate code is a computer programming term for a sequence of source code that occurs more than once either within a program or across different programs owned or maintained by the same entity duplicate code is generally considered undesirable for a number of reasons a minimum requirement is usually applied to the quantity of code that must appear in a sequence for it to be considered duplicate rather than coincidentally similar sequences of duplicate code are sometimes known as code clones or just clones the automated process of finding duplications in source code is called clone detection some of the ways in which two code sequences can be duplicates of each other are to be characterforcharacter identical characterforcharacter identical with white space characters and comments being ignored tokenfortoken identical tokenfortoken identical with occasional variation or functionally identical how duplicates are created some of the reasons why duplicate code may be created include copy and paste programming which in academic settings may be done as part of plagiarism or scrounging in which a section of code is copied because it works in most cases this operation involves slight modifications in the cloned code such as renaming variables or insertingdeleting code the language nearly always provides facilities to allow one copy of the code to serve multiple purposes but a copy is created due to the programmer not truly knowing the language not having the time to do it properly or not caring about the increased active software rot it may also contain functionality that is very similar to that in another part of a program is required and a developer independently writes code that is very similar to what exists elsewhere studies suggest that such independently rewritten code is typically not syntactically similar automatically generated code where having duplicate code may be desired to increase speed or ease of development is another reason for duplication note that the actual generator will not contain duplicates in its source code only the output it produces costs and benefits when code with a software vulnerability is copied the vulnerability may continue to exist in the copied code if the developer is not aware of such copies duplications can also be reduced using open source technologies for sharing code components instead of duplicating them between scm repositories detecting duplicate code a number of different algorithms have been proposed to detect duplicate code for example bakers algorithm rabinkarp string search algorithm using abstract syntax trees visual clone detection count matrix clone detection localitysensitive hashing antiunification example of functionally duplicate code consider the following code snippet for calculating the average of an array of integers extern int arraya extern int arrayb int suma 0 for int i 0 i 4 i suma arraya int averagea suma 4 int sumb 0 for int i 0 i 4 i sumb arrayb int averageb sumb 4 the two loops can be rewritten as the single function int calcaverageoffourint array int sum 0 for int i 0 i 4 i sum array return sum 4 using the above function will give source code that has no loop duplication extern int array1 extern int array2 int average1 calcaverageoffourarray1 int average2 calcaverageoffourarray2 note that in this trivial case the compiler may choose to inline both calls to the function such that the resulting machine code is identical for both the duplicated and nonduplicated examples above if the function is not inlined then the additional overhead of the function calls will probably take longer to run on the order of 10 processor instructions for most highperformance languages theoretically this additional time to run could matter example of duplicate code fix via code replaced by the method abstraction principle programming antipattern dont repeat yourself list of tools for static code analysis redundant code rule of three computer programming the university of alabama at birmingham code clones literature finding duplicate code in c vbnet aspx ruby python java c c actionscript or xaml 