如何管理null检查的冲击? [英] How can I manage the onslaught of null checks?
本文介绍了如何管理null检查的冲击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
很多时候,在编程中我们得到其中空
检查显示在特别大的数字的情况。我说这样的事情:
Quite often, in programming we get situations where null
checks show up in particularly large numbers. I'm talking about things like:
if (doc != null)
{
if (doc.Element != null)
{
... and so on
}
else
throw new Exception("Element cannot be null");
} else {
throw new Exception("document cannot be null");
}
基本上,整个事情变成不可读的噩梦,所以我想知道:是否有更简单的方式来描述我想要做以上? (除null检查,我得到的东西像 string.IsNullOrEmpty
不时。)
接受的回答:我接受了的这个链接因为描述的方法具有创新性,而且是precisely我想要什么。感谢肖恩!
Accepted answer: I accepted the answer that has this link because the approach described is innovative, and is precisely what I want. Thanks Shawn!
推荐答案
看看这篇文章:的一个流畅的方式到C#参数验证
这是写在Paint.NET开发商之一。他使用扩展方法来简化和清理空检查code。
It's written by one of the Paint.NET Developers. He uses extension methods to simplify and clean up null checking code.
这篇关于如何管理null检查的冲击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文