3

how can i refactor this code down to one method or something?

            if (!string.IsNullOrEmpty(_gridModel.Header))
                _gridModel.Header += ",";
            if (item != null)
                _gridModel.Header += item.Header;

            if (!string.IsNullOrEmpty(_gridModel.Width))
                _gridModel.Width += ",";
            if (item != null)
                _gridModel.Width += item.Width;

            if (!string.IsNullOrEmpty(_gridModel.Align))
                _gridModel.Align += ",";
            if (item != null)
                _gridModel.Align += item.Align;

            if (!string.IsNullOrEmpty(_gridModel.Filter))
                _gridModel.Filter += ",";
            if (item != null)
                _gridModel.Filter += item.Filter;

            if (!string.IsNullOrEmpty(_gridModel.Type))
                _gridModel.Type += ",";
            if (item != null)
                _gridModel.Type += item.Type;

            if (!string.IsNullOrEmpty(_gridModel.Sort))
                _gridModel.Sort += ",";
            if (item != null)
                _gridModel.Sort += item.Sort;

2 Answers 2

15

To start, refactor the logic into a function.

_gridModel.Header = AppendItem(_gridModel.Header, item == null ? null : item.Header);
_gridModel.Width = AppendItem(_gridModel.Width, item == null ? null : item.Width);
...
...

string AppendItem(string src, string item)
{
 if (! string.IsNullOrEmpty(src))
  src += ",";
 if (! string.IsNullOrEmpty(item))
  src += item;
 return src;
}

A good next step could be to use reflection and properties:

Edit: Fleshed out reflection solution, haven't actually debugged it yet though.

AppendProperties(_gridModel, item, "Header", "Width", "Align", ...)

void AppendProperty(object gridmodel, object item, params string[] propNames)
{
    foreach (string propName in propNames)
        AppendProperties(gridmodel, item, propName);
}

void AppendProperties(object gridmodel, object item, string propName)
{
    PropertyInfo piGrid = gridmodel.GetType().GetProperty(propName);
    if (piGrid != null && piGrid.PropertyType == typeof(string))
    {
        piGrid.SetValue(gridmodel, 
            piGrid.GetValue(gridmodel, null).ToString() + ",", null);
    }

    if (item == null) return;
    PropertyInfo piItem = item.GetType().GetProperty(propName);
    if (piItem != null)
    {
        piGrid.SetValue(gridmodel, 
            piGrid.GetValue(gridmodel, null).ToString() 
            + piItem.GetValue(item, null).ToString(), 
            null);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

+1 close enough, I might use a (ref string src, ...) and not return the value.
Would be faster, but I tend to default to no side effects when possible.
AppendItem(_gridModel.Header, item.Header); <- NullReferenceException when calling item.Header
I like your first step, I think it is actually better then the accepted answer. However, the use of reflection and magic strings in this context is, in my opinion, overkill.
Freddy: added check for item is null when calling the function, thanks.
7

Assuming you have .NET 3.5:

string Filter(string input, SomeType item, Func<SomeType, string> extract)
{
    if (!String.IsNullOrEmpty(input))
    {
        if (item == null) return ",";
        else return "," + extract(item);
    }
}

_gridModel.Header += Filter(_gridModel.Header, item, i => i.Header);
_gridModel.Width += Filter(_gridModel.Width, item, i => i.Width);
_gridModel.Align += Filter(_gridModel.Align, item, i => i.Align);

// etc...

2 Comments

Depending on your usage, with so many appends, it may give you better performance to use StringBuilder
Oh yeah if it's in a loop it might make more sense to pass a stringbuilder around instead of concatenation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.