Code inspection: Duplicated chained 'if' branches
This inspection identifies cases where multiple sequential if
statements have identical bodies. Such code increases redundancy, making maintenance harder and creating opportunities for introducing errors. Consolidating the conditions into a single statement simplifies the code and enhances readability.
In the example below, the bodies of the chained if
statements are identical. ReSharper suggests merging the conditions into a single statement to improve clarity and avoid repetition.
public void CheckStatus(int status)
{
if (status == 1)
{
Console.WriteLine("Action required");
}
else if (status == 2)
{
Console.WriteLine("Action required");
}
else if (status == 3)
{
Console.WriteLine("Action required");
}
}
public void CheckStatus(int status)
{
if (status == 1 || status == 2 || status == 3)
{
Console.WriteLine("Action required");
}
}
Last modified: 21 March 2025