IDR
IDR

Reputation: 1

Refactoring couple of identical methods using static content

So right now I have two basically identical methods each populating an attribute of my object.

Right now I am populating my model like this

        ConfigModel model = new ConfigModel();
        model.ConfigVarName = PopulateVarName(boxSize, firstDataRow);
        model.ConfigVarDesc = PopulateDescriptions(boxSize, firstDataRow);
        model.ConfigVarMaxValue = PopulateVarMaxValue(boxSize, firstDataRow);
        model.ConfigVarMinValue = PopulateVarMinValue(boxSize, firstDataRow);
        model.ConfigVarEngUnit = PopulateVarEngUnit(boxSize, firstDataRow);
        model.ConfigVarFormat = PopulateVarFormat(boxSize, firstDataRow);
        model.ConfigVarType = PopulateVarType(boxSize, firstDataRow);
        model.IOReference = PopulateIOReferences(boxSize, firstDataRow);
        model.TemplateName = PopulateTemplateName(firstDataRow);
        model.TagName = PopulateTagName(firstDataRow);
        model.SecurityGroup = PopulateSecurityGroup(firstDataRow);
        model.Area = PopulateArea(firstDataRow);
        model.Container = PopulateContainer(firstDataRow);
        model.ContainedName = PopulateContainedName(firstDataRow);

but calling each this many times doesn't feel right. Here's the giblet of the methods

    public List<string> PopulateVarName(List<int> boxSize, int firstDataRow)
    {
        List<string> names;
        string startCellForName = StaticColumns.configVarName + firstDataRow;
        int lastRowOfActualData = CalculateLastDataRowForInstance(firstDataRow, boxSize[0]);
        string fullRange = startCellForName + ":" + StaticColumns.configVarName + lastRowOfActualData;
        Xcl.Range rng = sheet.Range[fullRange];
        names = PopulateList(rng);
        return names;
    }

and

public string PopulateTagName(int firstDataRow)
    {

        string tagNameCell = StaticColumns.tagName + firstDataRow;
        string tagName = sheet.Range[tagNameCell].Value2;

        return tagName;
    }

So any advisory to get rid of these many lines and methods would be appreciated

Upvotes: 0

Views: 53

Answers (1)

Erik M&#252;ller
Erik M&#252;ller

Reputation: 104

Besides the two methods, which do not look "basically identical" to me, you should put all your member variable initialization into the constructor of your class:

class ConfigModel
{
    public ConfigModel(List<int> boxSize, int firstDataRow)
    {
        ConfigVarName = PopulateVarName(boxSize, firstDataRow);
        ConfigVarDesc = PopulateDescriptions(boxSize, firstDataRow);
        ConfigVarMaxValue = PopulateVarMaxValue(boxSize, firstDataRow);
        ConfigVarMinValue = PopulateVarMinValue(boxSize, firstDataRow);
        ConfigVarEngUnit = PopulateVarEngUnit(boxSize, firstDataRow);
        ConfigVarFormat = PopulateVarFormat(boxSize, firstDataRow);
        ConfigVarType = PopulateVarType(boxSize, firstDataRow);
        IOReference = PopulateIOReferences(boxSize, firstDataRow);
        TemplateName = PopulateTemplateName(firstDataRow);
        TagName = PopulateTagName(firstDataRow);
        SecurityGroup = PopulateSecurityGroup(firstDataRow);
        Area = PopulateArea(firstDataRow);
        Container = PopulateContainer(firstDataRow);
        ContainedName = PopulateContainedName(firstDataRow);
    }
}

And then create your model class like this

ConfigModel model = new ConfigModel(boxSize, firstDataRow);

EDIT: From the comments I suggest to do something like

Populate(List<int> boxSize, int firstDataRow, int category)
{
    [...]
    string fullRange;
    switch (category) 
    {
        case 1:
            fullRange = startCellForName + ":" + StaticColumns.configVarName + lastRowOfActualData;
            break;
        case 2:
            fullRange = startCellForName + ":" + StaticColumns.ConfigVarDesc + lastRowOfActualData; 
            break;

        [...]
    }
    [...]
}

Upvotes: 1

Related Questions