**EDIT: There are several options below that would work. Please vote/comment according to your views on the matter.
I'm working on cleaning up and adding functionality to a c# method with the following basic structure:
public void processStuff()
{
Status returnStatus = Status.Success;
try
{
bool step1succeeded = performStep1();
if (!step1succeeded)
return Status.Error;
bool step2suceeded = performStep2();
if (!step2suceeded)
return Status.Warning;
//.. More steps, some of which could change returnStatus..//
bool step3succeeded = performStep3();
if (!step3succeeded)
return Status.Error;
}
catch (Exception ex)
{
log(ex);
returnStatus = Status.Error;
}
finally
{
//some necessary cleanup
}
return returnStatus;
}
There are many steps, and in most cases step x must succeed in order to proceed to step x+1. Now, I need to add some functionality that will always run at the end of the method, but which depends on the return value. I'm looking for recommendations on how to cleanly refactor this for the desired effect. The obvious choice would be to put the functionality that depends on the return value in the calling code, but I'm not able to modify the callers.
One option:
public void processStuff()
{
Status returnStatus = Status.Success;
try
{
bool step1succeeded = performStep1();
if (!step1succeeded)
{
returnStatus = Status.Error;
throw new Exception("Error");
}
bool step2succeeded = performStep2();
if (!step2succeeded)
{
returnStatus = Status.Warning;
th