关于传递许多参数
我有大约 8-9 个参数来传递返回数组的函数。 我想知道是直接在函数中传递这些参数还是传递数组更好? 哪一个是更好的方法,为什么?
I have around 8-9 parameters to pass in a function which returns an array. I would like to know that its better to pass those parameters directly in the function or pass an array instead? Which will be a better way and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果我愿意做任何事情,那就是创建一个包含所有参数的结构,以获得良好的智能和强名称。
If I would do anything, then it would be to create an structure that holds all parameters to get nice intellisence and strong names.
单独传递它们,因为:
但是,如果参数确实是数组,则传递该数组。 示例:
对于如下所示的函数,请使用以下表示法:
对于如下所示的函数,请使用数组:
Pass them individually, because:
If the parameter really IS the array, though, then pass the array. Example:
For functions which look like this, use this notation:
For functions that look like this, use the array:
Martin Fowler 的重构书中的介绍参数对象重构涵盖了您的场景。 这本书非常值得拥有,但对于那些不值得拥有的人,重构是描述此处。 出版商网站和 Google 图书上也有预览版。 它建议不要用数组替换参数,而是用新对象替换。
Your scenario is covered by the Introduce Parameter Object refactoring in Martin Fowler's refactoring book. The book is well worth owning, but for those who don't, the refactoring is described here. There's also a preview on the publisher's site, and on Google books. It recommends replacing the parameters not with an array, but a new object.
关于 Skeets 对我上面的例子的评论,他会使用类而不是结构,并且可能会更清楚在哪里使用类以及在哪里使用结构,我也发布了这个。 我认为还有其他人也对此感到好奇。
正如我所见,使用类的主要原因是您可以使其不可变,但这对于结构也可能吗?
例如:
}
我很长时间以来都觉得我不再知道类和结构之间的区别了,因为我们现在可以拥有属性、初始化器、字段以及类在结构中拥有的所有内容。 我知道类是引用类型,结构是值类型,但是在上面的情况下将其用作函数中的参数时有什么区别?
我在网站 http://www.startvbdotnet.com/oop/ 上找到了差异的描述Structure.aspx 的描述正是我在脑海中映射它的方式:
也许这应该是一个自己的问题,但我觉得当我们对结构与类作为参数有不同的看法时,这是相关的。
Regarding Skeets comment on my example above that he would use a class instead of a structure and maybe make it clearer where to use a class and where to use a structure i post this too. I think there are other out there who are curious about this too.
The main reason to use a class as I could see was you could make it immutable, but thats possible with structures too?
for example:
}
I have long time felt that I dont know the differences anymore between classes and structures now when we can have propertys, initializers, fields and exactly everything that a class has in a structure too. I know classes are refernce types and structures are value types but what difference does it make in the case above when using it as a parameter in a function?
I found this description of the differences on the site http://www.startvbdotnet.com/oop/structure.aspx and that description is exactly how I mapped it in my head:
Maybe this should be a own question but I felt it was related when we all had different views on the structure vs class-thing as parameter.
我假设您使用的是 C# 4 并且可以只使用命名参数:
这使得代码几乎与 VB 或 Smalltalk 一样可读。 :-)
如果没有,我会同意戴夫·马克尔所说的。
I assume you're using C# 4 and can just use named parameters:
These make the code almost as readable as VB or Smalltalk. :-)
If not, I would go with what Dave Markle has to say.
如果这是会被大量使用的库代码,并且如果某些参数具有作为默认值候选的典型值,那么您应该考虑 Dave Markle 的建议,并提供参数逐渐减少的重载选择。 这是 Microsoft 框架设计指南中推荐的方法。
或者,您可以使用 Stefan 的方法获得类似的效果,通过使用成员初始值设定项设置默认值并使用一系列构造函数重载。
If this is library code that will see a lot of use, and if some of the parameters have typical values that are candidates for default values, then you should consider Dave Markle's advice, and provide a selectio of overloads with progressively fewer parameters. This is the approach recommended in the Microsoft Framework Design Guidelines.
Alternately, you can get a similar effect with Stefan's approach, by setting default values with member initializers and using a progression of ctor overloads.
如果您确实不想单独传递参数,我建议创建一个新类来封装所有参数。 为此,您可以(在 Java 中,很可能在 C# 中)在包含粗糙方法的类中声明一个公共内部类。 这可以避免出现实际上只是辅助类型的类。
If you really don't want to pass in your arguments separately I would suggest creating a new class which encapsulates all of your arguments. You can (in Java and most likely in C#) declare a public inner class inside the class containing the gnarly method for this purpose. This avoids having classes floating around which are really just helper types.
我想说也单独传递它们。 我不喜欢创建一个类,然后将该类作为参数传递的想法。 它是一种邮票耦合形式,这意味着更改将变得更加困难,因为一个类使用另一个类。 重用一个类意味着您也必须重用另一个类。
您可以使用接口来减少标记耦合,但这对我来说开销太大,所以这就是为什么我喜欢单独传递参数。
I would say pass them individually as well. I don't like the idea of creating a class, then passing that class through as an argument. Its a form of stamp coupling, which means making changes will be harder since one class uses the other. And reusing one class means you would have to reuse the other as well.
You could use an interface to reduce stamp coupling, but that's too much overhead for my tastes, so that's why I like to pass the arguments individually.
单个函数真的需要 8-9 个参数吗? 在我看来,如果您需要那么多参数,那么您可能在该函数中做了太多不同的事情。 尝试将代码重构为单独的函数,以便每个函数都有一个目的。
Do you really need 8-9 parameters for a single function? It seems to me that if you need that many parameters, then you're probably doing too many different things in that function. Try refactoring the code into separate functions so that each function has exactly one purpose.
除非函数作用于数组,否则不要将它们作为数组传递,出于以下原因,我不会创建新的数据结构来对参数进行分组
传递新的数据结构隐藏了函数真正需要的输入(它需要所有数据结构/部分数据吗?)
与 1 有关它使 UT 变得更加困难(当编写 UT 时,您需要重新创建整个数据结构)
如果输入参数不相关,您最终会得到一个新的数据结构,该数据结构将不相关的数据类型分组,只是为了使函数调用看起来更整洁
如果您选择将新数据结构传递给函数,则该函数不能在定义新数据结构的范围内使用
实际上,将每个参数传递给函数的唯一缺点是您可能无法将该函数放入一行代码,但不要忘记在函数调用之前需要的行,您将在其中填充数据结构。
Do not pass them as an array unless the function acts on an array, I wouldn't create a new data structure either to group the parameters for the following reasones
Passing a new data structure hides what the function really needs as input (does it need all the data structure/part of it?)
Related to 1 it makes UTs more difficult (when writing a UT you need to recreate the entire data structure)
If the input parameters are not related you end up with a new data structure that groups unrelated data types for no other reason than to make a function call look neater
If you chose to pass the new data structure to your function the function can not be used in a scope where the new datastructure was defined
Really the only disadvantage to passing each paramater to the function is that you might not be able to fit the function in one line of code, but don't forget the lines you need before the function call in which you will fill up your data structure.