**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;
throw new Exception("Warning");
}
//.. the rest of the steps ..//
}
catch (Exception ex)
{
log(ex);
}
finally
{
//some necessary cleanup
}
FinalProcessing(returnStatus);
return returnStatus;
}
This seems a little ugly to me. Instead, I could throw right from the performStepX() methods. However, this leaves the problem of setting the returnStatus variable appropriately in the catch block of processStuff(). As you may have noticed, the value returned on failure of a processing step depends on which step failed.
public void processStuff()
{
Status returnStatus = Status.Success;
try
{
bool step1succeeded = performStep1(); //throws on failure
bool step2succeeded = performStep2(); //throws on failure
//.. the rest of the steps ..//
}
catch (Exception ex)
{
log(ex);
returnStatus = Status.Error; //This is wrong if step 2 fails!
}
finally
{
//some necessary cleanup
}
FinalProcessing(returnStatus);
return returnStatus;
}
I would appreciate any suggestions that you might have.