在功能重复的参数检查 [英] Repeated parameter checks in functions

查看:111
本文介绍了在功能重复的参数检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在通话层次的所有方法都需要相同的参数。如果我dont't希望把它们放在实例级(类的成员),那么我问我始终如果meaningfull检查它们的有效性每种方法。

I often have call hierarchies in that all methods need the same parameters. If I dont't want to put them on the instance level (member of the class) then I ask me always if its meaningfull to check the validity of them in each method.

例如:

public void MethodA(object o){
   if(null == o){
      throw new ArgumentNullException("o");
   }
   // Do some thing unrelated to o

   MethodB(o);

   // Do some thing unrelated to o

}

public void MethodB(object o){
   if(null == o){
      throw new ArgumentNullException("o");
   }
   // Do something with o
}

如果东西方法 A使用参数,那么它的清晰,我也要有,也是检查的有效性MethdoB。但只要治法不执行任何更多的 0 不是给它方法b ,它是很好的做法,检查的有效性也是在治法

If Method A uses the parameter, then its clear, I have to check the validity there and also in MethdoB. But as long as MethodA does nothing more with o than give it to MethodB, is it good practice to check the validity also in MethodA.

治法也检查的爱维稳特可能是例外抛出的方法被调用方呼吁,这是好的,但是否有必要?调用堆栈也会注明。也许它在公共场合,内部,meaningfull保护,但不是私人的方法呢?

The avantage of checking also in MethodA may be that the exception throws in the method the callee has called, that is nice, but is it necessary? The call stack will state this also. Maybe its meaningfull in public,internal,protected but not in private methods?

我拿着空支票作为一个例子,也是指数验证或范围验证落于自我的问题,但是我觉得也有因为危险局限性冗余代码。你觉得呢?

I took the null-check as an example, but also index-validations or range validations fall in the self question, however I think there are limitations because of the danger of redundant code. What do you think?

更新

通过AakashM的答案,我有看到了,我是有点精确。 治法,不仅需要方法b ,它也做其他的事情,但没有涉及到 0 。我添加了一个例子来阐明这一点。谢谢AakashM

Through the answer of AakashM I have seen that I was to little precise. MethodA not only calls MethodB, it does also other things but not related to o. I added an example to clarify this. Thanks AakashM.

推荐答案

有关的概念史蒂夫·麦康奈尔的代码完成谈判路障 - 一个其中数据之外防守岬的墙是不可信的,且其内部的数据是可信的。数据要进入路障必须经过验证过程,但街垒中,数据可以自由走动奔放通过验证代码。

Steve McConnell's Code Complete talks about the concept of 'the barricade' - a wall of defensive-ness outside of which data is untrusted, and inside of which data is trusted. Data that wants to enter the barricade must go through a validation process, but within the barricade, data is free to move around untrammelled by validation code.

如果你能承担这一结构化量和项目分层,并坚持下去,它确实使里面最路障代码有较少的仪式,更本质。但是,只需要一种方法在路障的一切出问题打电话。

If you can impose this amount of structuring and layering in your project, and stick to it, it does make inside-the-barricade code have less ceremony and more essence. But it only takes one method to call over the barricade for everything to go wrong.

在你的榜样,方法b 公共。这意味着你有没有自动未来确保了治法将是其唯一的调用者 - 我会因此说,它的验证代码应该保持。如果是私人的类,但是,你可以把一个参数删除它。

In your example, MethodB is public. This means that you have no automatic future guarantees that MethodA will be its only caller - I would say therefore that its validation code should remain. If it were private to the class, however, you could make an argument for removing it.

至于治法,如果它实际的没有的不是打电话多个方法b ,它不应该存在。如果它是为将来的扩展存根,并在某些时候它会做的东西0 ,那么它的验证码也应保持不变。

As for MethodA, if it actually does nothing more than call MethodB, it shouldn't exist. If it's a stub for future expansion, and at some point it is going to do something with o, then its validation code should also remain.

这篇关于在功能重复的参数检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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