I'm working on a hobby project using Visual Studio Pro 2013 and Resharper. I often get warnings from Resharper like "This method contains too many statements" or "Member has cyclomatic complexity of 24 (120%)". Seeing these constantly makes me think I'm doing something wrong. What can I do? Following is my most recent. It adds and removes items from a list box to reflect the current state of the model. It walks two enumerators to do the set comparison. The method does one thing, and does it well. So why should it be too complex?
void Populate_listBoxProductions() {
listBoxProductions.BeginUpdate();
var currentItemsEnumerator = listBoxProductions.Items.GetEnumerator();
var newItemsEnumerator = _grammar.Productions.OrderBy(x => x.Name).GetEnumerator();
var doneCurrent = !currentItemsEnumerator.MoveNext();
var doneNew = !newItemsEnumerator.MoveNext();
var done = doneCurrent || doneNew;
while (!done) {
string currentItemName = (currentItemsEnumerator.Current as Grammar.Recognizer).Name;
string newItemName = newItemsEnumerator.Current.Name;
switch (Math.Sign(currentItemName.CompareTo(newItemName))) {
case -1:
listBoxProductions.Items.Remove(currentItemsEnumerator.Current);
doneCurrent = !currentItemsEnumerator.MoveNext();
break;
case 0:
doneCurrent = !currentItemsEnumerator.MoveNext();
doneNew = !newItemsEnumerator.MoveNext();
break;
case 1:
listBoxProductions.Items.Add(newItemsEnumerator.Current);
doneNew = !newItemsEnumerator.MoveNext();
break;
}
done = doneCurrent || doneNew;
}
if (!doneCurrent) {
do {
listBoxProductions.Items.Remove(currentItemsEnumerator.Current);
} while (currentItemsEnumerator.MoveNext());
}
if (!doneNew) {
do {
listBoxProductions.Items.Add(newItemsEnumerator.Current);
} while (newItemsEnumerator.MoveNext());
}
listBoxProductions.EndUpdate();
}