Sign in
Log inSign up
Carlton Upperdine

74 likes

·

10.0K reads

8 comments

Thomas Eyde
Thomas Eyde
May 30, 2022

How do I as a user of this interface/signature know that there are more expressive sub types? Because all I see is the base class, and there is little if anything that tells me there is more.

Actually something I experienced with the new Eventuous library for even sourcing, something failed, but I never new because nothing was thrown, hence nothing was logged.

Maybe a property less base type would help? No information available would be a hint to look for more. A Match(…) method taking lambdas for all variations would be better, but requires more work.

Too bad sum types are not here yet in C#.

2
·
·1 reply
Carlton Upperdine
Carlton Upperdine
Author
·May 30, 2022

This is absolutely an issue I have ran into due to C# lacking discriminated unions, though the OneOf library is pretty good in place of them.

I think in this case you'd have to actually look at the code emitting the results, so I tend to restrict this to the domain layer. I suppose you could also add XML comments to your public method detailing the various result types, though comments are only useful if they are up-to-date.

·
Ruben Scheedler
Ruben Scheedler
Apr 26, 2022

Thanks for the write-up! I really see the value of this pattern, as I often encounter the boolean return type being used. I will definitely keep this pattern in mind as a possible refactoring.

1
·
·1 reply
Carlton Upperdine
Carlton Upperdine
Author
·Apr 26, 2022

Thank you! I've used this pattern recently on a big refactor at work and it's been a huge help.

·
Siddharta Govindaraj
Siddharta Govindaraj
Apr 26, 2022

Great article. This technique is really nice for writing functional programming style code

1
·
·1 reply
Carlton Upperdine
Carlton Upperdine
Author
·Apr 26, 2022

Thank you 🙂 I actually got the idea from F#

1
·
Jakub Suchy
Jakub Suchy
May 5, 2022

I was just talking about this pattern at work today. I made a point about speed against exceptions, because throwing exception can be quite costly against newing up class instance of the result. Also with a result, the method has actually well defined interface of what it returns instead of returning various exceptions that are not visible anywhere from the outside when <summary>...<exception> is not defined. I couldn't persuade him. He didn't believe in my arguments. So I told him that I will try to benchmark newing vs throwing. Maybe that will persuade him. However having well defined outputs is more important for me. Because now we have wcf service that is throwing on every failure, but sometimes client doesn't catches the errors. How could it, when it doesn't know what all to catch :D

1
·
savvas pamboukas
savvas pamboukas
Jun 5, 2022

Really like this pattern! It actually fits one of my projects, will try to implement it next week. Another point that I think is against throwing exceptions is that your code should throw exceptions only in exceptional scenarios. The above examples (fradaulent activity, not enough funds, etc) are not exceptional scenarios.

·