限制每个方法的参数是多少? [英] Limit number of parameters per method?

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

问题描述

假设参数都是相同的类型,是否有拇指在问候的一种方法的参数数量的规则?我只是想知道我应该在哪里画线,我的选择是什么(即接口,阵列等)。

Assuming the parameters are all the same type, is there a rule of thumb in regards to the number of parameters for a method? Im just wondering where I should draw the line and what my alternatives are (ie interface, array, etc).

推荐答案

我会说这真的取决于你的情况。你做的事情对整个集?验证所有的项目或汇总数据,例如?在这种情况下,我会一个IEnumerable传递作为一个参数。

I would say it really depends on your case. Are you doing something to the entire set? Validating all the items or aggregating data, for example? In that case, I would pass in an IEnumerable as a single parameter.

传递大量的参数可以是关注点分离不佳的一个好兆头(即你的方法做太多),但它听起来像是在这种情况下,你在一个定义良好的组项目正在传递迭代他们以某种方式。鉴于C#3的集合初始化语法,我将在未来的参数列表,会是这样的 A型,B,键入c ...

Passing in a lot of parameters can be a good sign of poor separation of concerns (i.e. your method is doing too much), but it sounds like in this case you're passing in a well defined set of items to iterate them in some way. Given the collection initializer syntax in C# 3, I would recommend IEnumerable in pretty much every case over a list of parameters that would be something like Type a, Type b, Type c....

当然,如果你的参数实际上是区别对待,然后分离出来是有道理的,但我会考虑你在这种情况下做什么。我想到的一个简单的例子是建立一个树形数据结构和具有建立一个节点的子功能。一个可怜的语法可能是:

Of course, if your parameters are actually treated differently, then separating them out makes sense, but I would consider what you're doing in that case. A simple case that comes to mind would be building a tree data structure and having a function to build up the children of a node. A poor syntax might be:

Node BuildTree( Node parent, Node child1, Node child2...)

我可能会寻求更多的东西,如:

I would probably pursue something more like:

void ConstructChildren( this Node parent, IEnumerable<Node> children)

如果您可以提供更多的信息,你的情况下,虽然,和什么样的你在参数执行逻辑的,它可能会更容易,看看它是否是一个很好的候选人倒塌或重构。

If you can provide more information about your case, though, and what sort of logic you're performing on the parameters, it would probably be easier to see if it's a good candidate for collapsing or refactoring.

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

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