FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Duplicate_code

Duplicate code

In computer programming, duplicate code is multiple occurrences of equivalent source code in a codebase. A duplicate code fragment is also known as a code clone, and the process of finding clones in source code is called clone detection. Duplicate code has multiple undesirable aspects.

Whether fragments are classified as duplicate can be subjective. Fragments that are very small

Cost

edit

Code that includes duplicate functionality is more difficult to maintain because if it needs updating, there is a risk that only some of the duplicates will be updated, leaving the others as-is. When code with a vulnerability is duplicated, the vulnerability exists in the duplicate even after it is fixed in one copy. Refactoring to eliminate duplicate code can improve many software metrics, such as lines of code, cyclomatic complexity, and coupling. This may lead to shorter compilation time, lower cognitive load, less human error, and fewer forgotten or overlooked pieces of code.

However, not all code duplication can be refactored. Clones may be the most effective solution if the programming language provides inadequate or overly complex abstractions, particularly if supported with user interface techniques such as simultaneous editing. Furthermore, the risk of breaking code when refactoring may outweigh maintenance benefit. A study by Wagner, Abdulkhaleq, and Kaya concluded that while additional work must be done to keep duplicates in sync, if the programmers involved are aware of the duplicate code there weren't significantly more faults caused than in unduplicated code.intcalc_average_of_four(inta[]){ intsum=0; for(inti=0;i<4;i++){ sum+=a[i]; } returnsum/4; }

Using this function eliminates the duplicated code.

Arcelli Fontana, Francesca; Zanoni, Marco; Ranchetti, Andrea; Ranchetti, Davide (2013). "Software Clone Detection and Refactoring" (PDF). ISRN Software Engineering. 2013: 1–8. doi:10.1155/2013/129437.
  • ↑ Kapser, C.; Godfrey, M.W., ""Cloning Considered Harmful" Considered Harmful," 13th Working Conference on Reverse Engineering (WCRE), pp. 19-28, Oct. 2006
  • ↑ The University of Alabama at Birmingham: Code Clones Literature
  • Finding duplicate code in C#, VB.Net, ASPX, Ruby, Python, Java, C, C++, ActionScript, or XAML

  • View original on Wikipedia | Back | FazBrowse Home