Skip to main content
improved wording, expanded formulation, added result code, corrected naming for variables selectedExportPath→selectedExportType and csvFilePath→path
Source Link
amon
  • 12.6k
  • 36
  • 67
  1. Extract methodYou can use an Extract Method refactoring to get rid of the duplicate code:

    private static string GetExportFilePath(string csvFilePath, DateTime date, string fileSuffix) { return string.Format("{0}{1:yyyy-MM-dd}-{2}.csv", csvFilePath, date, fileSuffix); }

    private static string GetExportFilePath(string csvFilePath, DateTime date, string fileSuffix)
    {
        return string.Format("{0}{1:yyyy-MM-dd}-{2}.csv", csvFilePath, date, fileSuffix);
    }
    
  2. Also I would add those selectedExportPathselectedExportType and csvFilePathpath variables as parameters to the Export() method. I hope they are not used anywhere. It is usually pretty subjective whether to add something as method parameters or leave it as class member, but here I would definitely addpass them as parameters because I also tend to move such methods out from the views and. I assume this class is Viewa “View” since you have button click event handlers, and I also tend to move such methods out from the views.

If we use these two ideas, your event handlers would be simplified to:

private void btnBuildingList_Click(object sender, EventArgs e)
{
    var path = GetExportFilePath(csvFilePath, datDate.DateTime, "BuildingList.csv");
    Export((int)ExportType.Building, path);
}

private void btnOwnerList_Click(object sender, EventArgs e)
{
    var path = GetExportFilePath(csvFilePath, datDate.DateTime, "OwnerList.csv");
    Export((int)ExportType.Persons, path);
}

private void btnFacts_Click(object sender, EventArgs e)
{
    var path = GetExportFilePath(csvFilePath, datDate.DateTime, "FactsData.csv");
    Export((int)ExportType.Facts, path);
}
  1. Extract method:

    private static string GetExportFilePath(string csvFilePath, DateTime date, string fileSuffix) { return string.Format("{0}{1:yyyy-MM-dd}-{2}.csv", csvFilePath, date, fileSuffix); }

  2. Also I would add those selectedExportPath and csvFilePath as parameters to Export method. I hope they are not used anywhere. It is usually pretty subjective whether to add something as method parameters or leave it as class member, but here I would definitely add them as parameters because I also tend to move such methods out from the views and I assume this class is View since you have button click event handlers.

  1. You can use an Extract Method refactoring to get rid of the duplicate code:

    private static string GetExportFilePath(string csvFilePath, DateTime date, string fileSuffix)
    {
        return string.Format("{0}{1:yyyy-MM-dd}-{2}.csv", csvFilePath, date, fileSuffix);
    }
    
  2. I would add those selectedExportType and path variables as parameters to the Export() method. I hope they are not used anywhere. It is usually pretty subjective whether to add something as method parameters or leave it as class member, but here I would definitely pass them as parameters. I assume this class is a “View” since you have button click event handlers, and I also tend to move such methods out from the views.

If we use these two ideas, your event handlers would be simplified to:

private void btnBuildingList_Click(object sender, EventArgs e)
{
    var path = GetExportFilePath(csvFilePath, datDate.DateTime, "BuildingList.csv");
    Export((int)ExportType.Building, path);
}

private void btnOwnerList_Click(object sender, EventArgs e)
{
    var path = GetExportFilePath(csvFilePath, datDate.DateTime, "OwnerList.csv");
    Export((int)ExportType.Persons, path);
}

private void btnFacts_Click(object sender, EventArgs e)
{
    var path = GetExportFilePath(csvFilePath, datDate.DateTime, "FactsData.csv");
    Export((int)ExportType.Facts, path);
}
deleted 12 characters in body
Source Link
Snowbear
  • 3.7k
  • 1
  • 19
  • 23
  1. Extract method:

    private static string GetExportFilePath(string csvFilePath, DateTime date, string fileSuffix) { return string.Format("{0}{1:yyyy-MM-dd}-{2}.csv", csvFilePath, datDate.DateTimedate, fileSuffix); }

  2. Also I would add those selectedExportPath and csvFilePath as parameters to Export method. I hope they are not used anywhere. It is usually pretty subjective whether to add something as method parameters or leave it as class member, but here I would definitely add them as parameters because I also tend to move such methods out from the views and I assume this class is View since you have button click event handlers.

  1. Extract method:

    private static string GetExportFilePath(string csvFilePath, DateTime date, string fileSuffix) { return string.Format("{0}{1:yyyy-MM-dd}-{2}.csv", csvFilePath, datDate.DateTime, fileSuffix); }

  2. Also I would add those selectedExportPath and csvFilePath as parameters to Export method. I hope they are not used anywhere. It is usually pretty subjective whether to add something as method parameters or leave it as class member, but here I would definitely add them as parameters because I also tend to move such methods out from the views and I assume this class is View since you have button click event handlers.

  1. Extract method:

    private static string GetExportFilePath(string csvFilePath, DateTime date, string fileSuffix) { return string.Format("{0}{1:yyyy-MM-dd}-{2}.csv", csvFilePath, date, fileSuffix); }

  2. Also I would add those selectedExportPath and csvFilePath as parameters to Export method. I hope they are not used anywhere. It is usually pretty subjective whether to add something as method parameters or leave it as class member, but here I would definitely add them as parameters because I also tend to move such methods out from the views and I assume this class is View since you have button click event handlers.

Source Link
Snowbear
  • 3.7k
  • 1
  • 19
  • 23

  1. Extract method:

    private static string GetExportFilePath(string csvFilePath, DateTime date, string fileSuffix) { return string.Format("{0}{1:yyyy-MM-dd}-{2}.csv", csvFilePath, datDate.DateTime, fileSuffix); }

  2. Also I would add those selectedExportPath and csvFilePath as parameters to Export method. I hope they are not used anywhere. It is usually pretty subjective whether to add something as method parameters or leave it as class member, but here I would definitely add them as parameters because I also tend to move such methods out from the views and I assume this class is View since you have button click event handlers.