Sean G. Wright Profile picture
Jun 2 10 tweets 5 min read
A Thread 🧵

In this video from @nickchapsas, he recommends _not_ throwing exceptions in #csharp and instead using the "Result" type (a monad 🧐) to model failures.

I've been using this approach for several years now and I can say it _works very well_!

I have multiple blog posts and presentations talking about this topic and the topic of modeling failures/optional data in #csharp. These links are mostly in the context of @KenticoXP, the platform @wiredviews uses to build websites for our clients.

(links at the end)
Here are some of my thoughts on why we should use Result vs Exception for modeling failures.

Don't forget 🤔, we use language features to model real world events in code, so we should consider the tools we have available and what their merits/costs are...
Exceptions don't show up in C# method signatures!

Exceptions are always a surprise to consumers of a method (XML doc comments don't suffice 😑).

They require extremely defensive coding and need to be caught low-level to respond in an effective way...
Exceptions are leaky 💦 abstractions!

Since method signatures don't include exceptions they throw, programmers need to look inside the methods to know which exceptions to catch, especially ones we want to recover from.

What's the point of encapsulation if every capsule leaks?
Catching exceptions at the top of an application (eg #aspnetcore middleware) is too far away from the cause or too late in the request process to recover.

Using middleware is basically a recipe 📃 to convert Exceptions into HTTP status codes, which doesn't bring business value..
Try/Catch is not ergonomic 😫!

It requires programmers to either initialize variables to a default (or null) value outside of the try/catch to make them available later, or move the try/catch into a method so values can be returned (vs stored in variables).
Exceptions are addictive!

Since they _can_ be used for flow control without modifying consuming code, we often end up continuing the exception-as-flow-control pattern throughout our code - it's just easier 😅.

This results in code logic that is much harder to understand.
Links:

Modeling missing data: Robust data access with Kentico Xperience


Handling Failures - Centralized Exception Handling
dev.to/seangwright/ke…

Handling Failures - Return Errors, Don't Throw Them
dev.to/seangwright/ke…
Links:

Modeling Missing Data / The Maybe Monad


My favorite "Result" C# library
github.com/vkhorikov/CSha…

A TypeScript adaptation I've authored
github.com/seangwright/ty…

LanguageExt
github.com/louthy/languag…

Understanding LanguageExt
github.com/stumathews/Und…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Sean G. Wright

Sean G. Wright Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(