0

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();
}

Edited by brent.lewis.39

2
Contributors
1
Reply
12
Views
3 Years
Discussion Span
Last Post by ddanbe
0

Hi Brent welcome!
Working with GetEnumerator and MoveNext is a bit old style I guess.
The 2013 C# compiler is smart enough to figure that out for you.
Use something like this:(just a pseudocode)

foreach (string s in itemstoremove)
{
    mylistBox.Items.Remove(s);
}

Your method is also doing a lot of things adding, removing...

Edited by ddanbe: addition

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.