Occassinally I have to write methods like this:
string GetReportOutputDirectoryAndMakeSureExist()
{
string path = Path.Combine ( ... ) //whatever logic
if(!Directory.Exists(path)) Directory.Create(path);
return path;
}
or
string GetAndVerifyExistenceOfReportConfigurationPath()
{
string path = Path.Combine ( ... ) //whatever logic to find the configuration
if(!File.Exists(path)) throw new Exception("Report configuration not found");
return path;
}
or
Customer GetCustomerAndVerifyActive(int id)
{
Customer customer = Db.GetCustomerById(id);
if(!customer.IsActive) throw new Exception("Customer is not active");
return customer;
}
Is it a good practice? I am told that it is normally not a good idea for a method to do more than one things, or for a method to have side-effects (like creating directory). But if I split, for example the last metod to GetCustomer(id) and VerifyActive(customer), I will have to do:
var customer = GetCustomer(id);
VerifyActive(customer);
consecutively at several places, and I think it counts as violation of DRY. Is this a good idea? Any idea how to help with the long method names?