Chapter 7. Functional Flow
Calls to external systems such as databases and web APIs are an absolute pain, aren’t they? Before using your data—the most important part of the function you’re writing—you have to do the following:
-
Catch and handle any exceptions. Maybe the network was glitching, or the database server was offline?
-
Check that what has come back from the database is not
null
. -
Check that there’s an actual, reasonable set of data, even if it isn’t
null
.
This entails a lot of tedious boilerplate, which gets in the way of your business logic.
Use of the Maybe
DU from the previous chapter will help somewhat with returning something other than null
for records not found or errors encountered, but boilerplate is still required even then.
What if I were to tell you there is a way to never have to see another unhandled exception again? Not only that, but you’d never even need to use a try
/catch
block again. As for null
checks? Forget ’em. You won’t ever have to do that again either.
Don’t believe me? Well, strap in; I’m going to introduce you to one of my favorite FP features. I use it all the time in my day job, and I’m hoping that after reading this chapter, you might too.
Maybe, Revisited
I’d like to revisit that Maybe
DU from Chapter 6 now, but this time I’m going to show you how it can be even more useful than you could ever have imagined.
What I’m going to do is walk you through adding in a version of the Map()
extension method from a couple of chapters ago. As ...
Get Functional Programming with C# now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.