I just finished coding a class, and realized that all of its public functionality is encapsulated in a single method. It has no properties, no shared resources, and requires no constructor overload and handles disposal of anything it uses. It looks something like this:
public class ReportGenerator
{
public string GenerateReport(List<SomeClass> stuffToReportOn)
{
string fileName = String.Empty;
using(var reportDs = CreateDataSet(stuffToReportOn))
{
//do the stuff with the third party tool that
//creates the report.
//Construct the filename.
//Save the report.
}
return fileName;
}
private TypedDataSetRequiredByThirdPartyLib CreateDataSet(List<SomeClass> reportItems)
{
//create the dataset, calling two other private methods
//to build the tables/rows
}
}
After I finished refactoring it, I realized that this class could be entirely static. My question is, should it? Should a class that encapsulates all of its functionality in one public method be made static?
No. and what are the expected benefits?
Far more important is the potential for errors. If in your class you declare a static variable, it’ll only get initialised once, its value will persist and each call will potentially behave differently. Easily over-looked, this can be difficult to debug.