Need help choosing Result or Maybe
macfarmw opened this issue · 2 comments
I am using the extensions for a method that gets some data from another system.
The 3 possible results of calling this method are:
- The data is found and returned.
- No data is found for the given parameter values but the connection and query worked.
- The database connection and/or query failed for some unexpected reason so we don't know if the data exists or not.
If I use Maybe I can handle case 1 and 2 but case 3 is not expressed in the signature.
If I use Result I can handle 1 and 3 but I'm not sure how to handle case 2.
I guess it is possible to use Result<Maybe<DataObject>>. Is that the best option?
What are the guidelines for choosing Maybe versus Result?
Thanks,
Matthew
Hi Matthew,
Yes, Result<Maybe<DataObject>>
is exactly what I recommend you use, and that is what I use myself in similar situations.
The guideline here is that if the outcome of the operation is stable, meaning that it depends only on the presence of the data in the external system, use Maybe
. If the outcome may vary and depends on some secondary attributes, such as network connectivity, use Result
. Use Result<Maybe>
if you expect both of these at the same time.
Another way to look at it: if you expect some operation to fail due to reasons that are not related to the callee itself, use Result
to state that information in the method signature.
Hope that helps,
Vladimir
Thanks for your reply. Yes that is very helpful. I also found your blog on functional error handling which covers this topic well.