验证参数的正确顺序是什么?
作为参考,我想指出 这个问题被考虑到如何测试参数。
让我们以以下两个片段为例:
public void DoSomething1(Dictionary<string, string> param1, Object param2)
{
if (null == param1)
throw new ArgumentNullException("param1");
if(!param1.Keys.Contains("somekey"))
throw new ArgumentException("param1 must contain key \"somekey\"");
if(null == param2)
throw new ArgumentNullException("param2");
// now we do some stuff
}
public void DoSomething2(Dictionary<string, string> param1, Object param2)
{
if (null == param1)
throw new ArgumentNullException("param1");
if (null == param2)
throw new ArgumentNullException("param2");
if(!param1.Keys.Contains("somekey"))
throw new ArgumentException("param1 must contain key \"somekey\"");
// now we do some stuff
}
两种方法都会对方法的内部工作实现相同的保护。
我的问题是...
测试传递给方法的参数(例如本例)的正确方法/顺序是什么以及为什么?
请引用参考文献来支持您的答案。
For reference, I would like to note that this question is taken into consideration for how the prarameters are tested.
Lets take the following two snippets as an example:
public void DoSomething1(Dictionary<string, string> param1, Object param2)
{
if (null == param1)
throw new ArgumentNullException("param1");
if(!param1.Keys.Contains("somekey"))
throw new ArgumentException("param1 must contain key \"somekey\"");
if(null == param2)
throw new ArgumentNullException("param2");
// now we do some stuff
}
public void DoSomething2(Dictionary<string, string> param1, Object param2)
{
if (null == param1)
throw new ArgumentNullException("param1");
if (null == param2)
throw new ArgumentNullException("param2");
if(!param1.Keys.Contains("somekey"))
throw new ArgumentException("param1 must contain key \"somekey\"");
// now we do some stuff
}
Both methods will accomplish the same protection to the inner workings of the method.
My Question Is...
What is the correct approach / order to test parameters passed to a method (such as in this example) and why?
Please cite references to backup your answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不存在“正确的顺序”。
它完全是特定于实现的。换句话说,对您来说,报告参数为空更重要,还是在获取参数时对其进行深入检查更重要?
如果我这样做,我会采取“绩效”方法。换句话说,我将首先测试所有必要的参数是否为非空,然后我将打开每个参数以确保我得到了我想要的。执行空引用检查比测试字典是否包含键要快(稍微)。
当然,我唯一担心的是这个方法是否注定每秒被调用数百次并且它有可能传递空参数。对于影响有限的方法,我会坚持调用者首先需要了解的内容。
There is no "correct order".
It's completely implementation specific. In other words is it more important to you to report back that the parameters are null OR is it more important to do deep inspection of the parameters as you get to them?
If I was doing this I would take the "performance" approach. In other words I would first test that all necessary parameters were non null, then I would open up each parameter to make sure I got what I wanted. It's faster (slightly) to do the null reference checks than it is to test whether a dictionary contains a key.
Of course, the only time I'd even worry about it is if this method was destined to be called hundreds of times a second and it had a likelihood of being passed null parameters. For methods that have limited impact I would stick with what the caller needed to know first.