是否有可能将参数验证视为多余的情况? [英] Is there a case where parameter validation may be considered redundant?

查看:90
本文介绍了是否有可能将参数验证视为多余的情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在公共方法中要做的第一件事是在每个参数都没有被使用,传递或引用的机会之前对其进行验证,然后在其中任何一个违反合同时引发异常.我发现这是一个很好的做法,因为它可以让您在违规行为发生时立即抓住违规者,但随后,我经常会编写一个非常简单的getter/indexer,如下所示:

The first thing I do in a public method is to validate every single parameter before they get any chance to get used, passed around or referenced, and then throw an exception if any of them violate the contract. I've found this to be a very good practice as it lets you catch the offender the moment the infraction is committed but then, quite often I write a very simple getter/indexer such as this:

private List<Item> m_items = ...;

public Item GetItemByIdx( int idx )
{
    if( (idx < 0) || (idx >= m_items.Count) )
    {
        throw new ArgumentOutOfRangeException( "idx", "Invalid index" );
    }

    return m_items[ idx ];
}

在这种情况下,index参数直接与列表中的索引相关,而且我知道一个事实(例如文档),列表本身将完全相同,并且会抛出相同的异常.我应该删除此验证,还是最好不进行验证?

In this case the index parameter directly relates to the indexes in the list, and I know for a fact (e.g. documentation) that the list itself will do exactly the same and will throw the same exception. Should I remove this verification or I better leave it alone?

我想知道你们的想法,因为我现在正在重构一个大项目,并且发现了许多类似上面的情况.

I wanted to know what you guys think, as I'm now in the middle of refactoring a big project and I've found many cases like the above.

谢谢.

推荐答案

这不仅仅是品味问题,请考虑

It's not just a matter of taste, consider

if (!File.Exists(fileName)) throw new ArgumentException("...");            
var s = File.OpenText(fileName);

这看起来与您的示例相似,但是出于多种原因(并发,访问权限),即使出现FileNotFound错误,OpenText()方法仍可能失败.因此,存在检查只是给人一种错误的安全感和控制感.

This looks similar to your example but there are several reasons (concurrency, access rights) why the OpenText() method could still fail, even with a FileNotFound error. So the Exists-check is just giving a false feeling of security and control.

这是一种思维定势的东西,当您编写GetItemByIdx方法时,它看起来很明智.但是,如果您浏览随机的一段代码,通常可以进行许多假设,然后再继续进行操作.一遍又一遍地检查它们是不切实际的.我们必须要有选择性.

It is a mind-set thing, when you are writing the GetItemByIdx method it probably looks quite sensible. But if you look around in a random piece of code there are usually lots of assumptions you could check before proceeding. It's just not practical to check them all, over and over. We have to be selective.

因此,在像GetItemByIdx这样的简单传递方法中,我会反对重复检查.但是只要函数增加了更多功能,或者有一个非常明确的规范说出关于idx的东西,该论点就会转过来.

So in a simple pass-along method like GetItemByIdx I would argue against redundant checks. But as soon as the function adds more functionality or if there is a very explicit specification that says something about idx that argument turns around.

根据经验,当定义明确的条件被破坏并且该条件与当前级别相关时,应该引发异常.如果条件属于较低级别,请让该级别处理.

As a rule of thumb an exception should be thrown when a well defined condition is broken and that condition is relevant at the current level. If the condition belongs to a lower level, then let that level handle it.

这篇关于是否有可能将参数验证视为多余的情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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