凡验证方法的参数? [英] Where to validate method's arguments?

查看:207
本文介绍了凡验证方法的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,在那里 - 和频率 - 在code validate方法的参数。

在下面(一个.dll库)的例子类,你觉得是最好的方式?假设我想验证,有些对象不能是(但可以要求法正常运行的任何其他验证)。它是更好地在点1,检查它只有一次,在公众用户的方法avaible,以及后来的相信自己,在其他私人的,方法也不会为空,或者最好是有点偏执,并检查它的每一个时间它会被使用(在点2 3和4)

检查它只是使用对象之前(以磅为2,3,4)保护我在未来,如果我决定要改变的东西在课堂上,利用这些私有方法,而忘记通过有效的对象。我也没有记住有关验证,如果我在未来增加一些新的公共方法。在另一方面 - 这是检查相同的条件下一遍又一遍。或者你可能有一些其他的建议?

 公共类MyClass的()
{
    公共MyClass的()
    {
    }

    公共无效ProcessObject(SomeObject OBJ)
    {
        // 1。如果(OBJ == NULL)抛出新的ArgumentException(你必须提供有效的对象。);

        DoSomething的(OBJ);
        DoMore(OBJ);
        DoSomethingElse(OBJ);
    }

    私人无效的doSomething(SomeObject OBJ)
    {
        // 2。如果(OBJ == NULL)抛出新ArgumenException(你必须提供有效的对象。);
        //做些事情OBJ ...
    }

    私人无效DoMore(SomeObject OBJ)
    {
        // 3。如果(OBJ == NULL)抛出新的ArgumentException(你必须提供有效的对象。);
        //做些事情OBJ ...
    }

    私人无效DoSomethingElse(SomeObject OBJ)
    {
        // 4。如果(OBJ == NULL)抛出新的ArgumentException(你必须提供有效的对象。);
        //做些事情OBJ ..
    }
}
 

解决方案

如果那就是你暴露的其他开发人员消耗,然后在您的每一个方法的API,你的确应该抛出或者的ArgumentException ArgumentNullException

如果它的内部类或方法,其他开发商将不会与直接的互动,我会用 Debug.Assert的,所以在调试模式下,你获得额外的调试信息,在发行模式下,它被nopped路程,您收到没有做这些检查的说法随处可见的性能优势。

I'm wondering, where - and how often - in the code validate method's arguments.

In the example class below (a .dll library), what do you think is the best way? Suppose I want to validate, that some object cannot be null (but it can be any other validation required for method to run properly). Is it better to check it only once in point 1., in public method avaible for user, and later "trust myself", that in other, private, methods it will not be null or, better be a little paranoid and check it every time it's going to be used (in points 2. 3. and 4.)

Checking it just before using the object (in points 2, 3, 4) protects me in the future, if I decide to change something in the class, using these private methods, and "forget" to pass valid object. Also I don't have to remember about validation if I add some new public method in the future. On the other hand - it's checking for the same condition over and over again. Or maybe you have some other suggestions?

public class MyClass()
{
    public MyClass()
    {
    }

    public void ProcessObject(SomeObject obj)
    {
        //1. if (obj == null) throw new ArgumentException("You must provide valid object.");

        DoSomething(obj);
        DoMore(obj);
        DoSomethingElse(obj);
    }

    private void DoSomething(SomeObject obj)
    {
        //2. if (obj == null) throw new ArgumenException("You must provide valid object.");
        //do something with obj...
    }

    private void DoMore(SomeObject obj)
    {
        //3. if (obj == null) throw new ArgumentException("You must provide valid object.");
        //do something with obj...
    }

    private void DoSomethingElse(SomeObject obj)
    {
        //4. if (obj == null) throw new ArgumentException("You must provide valid object.");
        //do something with obj..
    }
}  

解决方案

If it's an API that you're exposing for other developers to consume, then on each of your methods you should indeed throw either ArgumentException or ArgumentNullException.

If it's internal classes or methods that other developers will not be interacting with directly, I would use Debug.Assert, so in debug mode you get additional debugging information, and in release mode it gets nopped away and you receive the performance benefit of not doing all these argument checks everywhere.

这篇关于凡验证方法的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆