(Practically) every application source code contains clones. A clone has multiple clone instances, which are similar logic at different locations in the source code.
The existence of clones breaks the DRY principle and increases the effort for testing and maintaining the application. Another disadvantage of having many clones in your application is that there is more code that you or other contributors have to read and understand.
There are four different types of clones.
Instances of a type-1 clone can differ in comments, spaces and layout.
Example: The following two functions are instances of a type-1 clone.
function numberIsEven(int $number): bool
{$isEven = ($number % 2) === 0;
return $isEven;
}
function numberIsEven(int $number): bool
{// Use modulo to check if number is
// dividable by 2
$isEven
= ($number % 2) === 0;
return $isEven;
}
Instances of a type-2 clone have all characteristics of type-1 clones and can additionally differ in the names of identifiers and in literals.
Example: The following two functions are instances of a type-2 clone.
function numberIsEven(int $number): bool
{$isEven = ($number % 2) === 0;
return $isEven;
}
function numberIsNotOdd(int $number): bool
{// Use modulo to check if number is
// dividable by 2
$isNotOdd
= ($number % 2) === 0;
return $isNotOdd;
}
Instances of a type-3 clone have all characteristics of type-2 clones. Furthermore, there can be added or removed statements in one of the clone instances.
Example: The following two functions are instances of a type-3 clone.
function numberIsEven(int $number): bool
{$isEven = ($number % 2) === 0;
return $isEven;
}
function numberIsNotOdd(int $number): bool
{// Use modulo to check if number is
// dividable by 2
$isNotOdd
= ($number % 2) === 0;
print($isNotOdd);
return $isNotOdd;
}
Instances of a type-4 clone can differ completely in their syntax. Only the semantic has to stay the same between the clone instances.
Example: The following two functions are instances of a type-4 clone.
function numberIsEven(int $number): bool
{$isEven = ($number % 2) === 0;
return $isEven;
}
function isNumberValid(int $a): bool
{$half = 0.5 * $a;
return floor($half) === ceil($half);
}