duplication. Costs and benefits Code which includes duplicate functionality is more difficult to support because, it is simply longer, and if it needs updating, there is a danger that one copy of the code will be updated without further checking for the presence of other instances of the same code. On the other hand, if one copy of the code is being used for different purposes, and it is not properly documented, there is a danger that it will be updated for one purpose, but this update will not be required or appropriate to its other purposes. These considerations are not relevant for automatically generated code, if there is just one copy of the functionality in the source code. In the past, when memory space was more limited, duplicate code had the additional disadvantage of taking up more space, but nowadays this is unlikely to be an issue. 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. Refactoring duplicate code can improve many software metrics, such as lines of code, cyclomatic complexity, and coupling. This may lead to shorter compilation times, 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 risks of breaking code when refactoring may outweigh any maintenance benefits. 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. Detecting duplicate code A number of different algorithms have been proposed to detect duplicate code. For example: Baker's algorithm. Rabin–Karp string search algorithm. Using Abstract Syntax Trees. Visual clone detection. Count Matrix Clone Detection. Locality-sensitive hashing Anti-unification Example of functionally duplicate code Consider the following code snippet for calculating the average of an array of integers extern int array_a[]; extern int array_b[]; int sum_a = 0; for (int i = 0; i < 4; i++) sum_a += array_a[i]; int average_a = sum_a / 4; int sum_b = 0; for (int i = 0; i < 4; i++) sum_b