Chained null checks and the Maybe monad

added by sdorman
9/12/2010 8:26:05 AM


A great many programmers have met a situation where, while accessing a nested object property (e.g., person.Address.PostCode), they have to do several null checks. This requirement frequently pops up in XML parsing where missing elements and attributes can return null when you attempt to access them (and subsequently trying to access Value throws a NullReferenceException). In this article, Dmitri shows how a take on the Maybe monad in C#, coupled with use of extension methods, can be used to improve readability.