I have lots of code which is doing some data processing (in C# to be more specific). Very often the data may be only processed, if some criteria are met. Since the criteria can be rather complex, they are checked in a lazy fashion. They are not checked beforehand. Thus: If during the processing some criterion is not matching, the processing needs to be cancelled or shortcutted.
Since this can occure quite frequently and there are cases where this is considered to be nothing exceptional, I work with return values and with a pattern like:
if (string.IsNullOrEmpty(customerSecondaryAddress))
{
LogCustomerHasNoSecondaryAddress(entry);
return ProcessingStatus.IsProcessed;
}
ProcessSecondaryAddress(customerSecondaryAddress);
return ProcessingStatus.Continue;
I was hesitant to use Excpetions instead, because not matching criteria like in the example above is nothing exceptional. Besides it yields method signatures, which communicate quite clearly their purpose:
public ProcessingStatus WriteRecipientListFor(Customer customer)
The problem is, that I have to pass the status code around (ProcessingStatus
). This is especially cumbersome, when the processing logic is quite nested. In this case I need to pass the status code up the callstack and return
statements are scattered around in the code.
My questions are: Is the return value approach appropriate? Should I switch to Exceptions instead? Are there other patterns or approaches which I can use instead?