It could be called IfNotNull or Maybe, currently i'm going with Maybe.
Project available at http://Maybe.codeplex.com
So we've all had to write code like
if(one==null) return null;
if(one.two==null) return null;
if(one.two.three==null) return null;
return one.two.three.four;
Now there's code to do it all in one fell swoop.
return one.Maybe(o=>o.two.three.four);
It also handles primitives not just classes:
return one.MaybeNullable(o=>o.two.three, t=>t.four);
Source
Inspired by:
http://blogs.msdn.com/alexj/archive/2008/03/03/maybe-there-is-more.aspx
and
http://blogs.developpeur.org/miiitch/archive/2008/02/29/vendredi-c-est-expression-tree.aspx
Some reading on the subject:
http://abdullin.com/journal/2009/10/6/zen-development-practices-c-maybe-monad.html
http://stackoverflow.com/questions/1196031/evil-use-of-maybe-monad-and-extension-methods-in-c
Note to self: Answer this once posted: http://stackoverflow.com/questions/854591/how-to-check-for-nulls-in-a-deep-lambda-expression/854619#854619
When Abstractions Break
5 weeks ago