Code inspection: Duplicated statements
This inspection detects repeated statements inside conditional branches, such as if
/else
or switch
blocks. Repeated logic in these structures can adversely affect code readability and maintainability. Removing the duplicated statements can simplify the code, making it less error-prone and easier to adapt to changes.
Consider the example below, where the same statements are executed in both branching flows. Removing the duplicated statements from within the if
statement does not change the semantics, but greatly improves clarity and readability of the code.
public string CheckResult(bool result)
{
if (result)
{
Console.WriteLine("Checking result");
return "OK";
}
Console.WriteLine("Checking result");
return "OK";
}
public string CheckResult(bool result)
{
if (result)
{
}
Console.WriteLine("Checking result");
return "OK";
}
Last modified: 24 March 2025