How to reduce Cyclomatic complexity in an if-else statement
c#, cyclomatic-complexity
Solution
It looks like you perform the same logic on each "TextBox" (at least I think they are TextBoxes). I would recommend putting all of them into a collection and performing the following logic:
// Using var, since I don't know what class Name and Age actually are
// I am assuming that they are most likely actually the same class
// and at least share a base class with .Text and .BackGround
foreach(var textBox in textBoxes)
{
// Could use textBox.Text.Length > 0 here as well for performance
if(textBox.Text == string.Empty)
{
textBox.Background = Brushes.LightSteelBlue;
}
}
Note: This does change your code a bit, as I noticed you only check the value of one "TextBox" only if the previous ones did not have empty text. If you want to keep this logic, just put a `break;` statement after `textBox.Background = Brushes.LightSteelBlue;` and only the first empty "TextBox" will have its background color set.
Problem
What will you do in this case to reduce the Cyclomatic Complexty ``` if (Name.Text == string.Empty) Name.Background = Brushes.LightSteelBlue; else if(Age.Text == string.Empty) Age.Background = Brushes.LightSteelBlue; else if(...) ... else { // TODO - something else } ``` Let suppose I have 30 or more.