关于传递许多参数

发布于 2024-07-09 12:02:47 字数 74 浏览 8 评论 0原文

我有大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

老娘不死你永远是小三 2024-07-16 12:02:47

如果我愿意做任何事情,那就是创建一个包含所有参数的结构,以获得良好的智能和强名称。

public struct user 
{ 
    public string FirstName; 
    public string LastName; 
    public string zilionotherproperties; 
    public bool SearchByLastNameOnly; 
} 
public user[] GetUserData(user usr) 
{ 
    //search for users using passed data and return an array of users. 
} 

If I would do anything, then it would be to create an structure that holds all parameters to get nice intellisence and strong names.

public struct user 
{ 
    public string FirstName; 
    public string LastName; 
    public string zilionotherproperties; 
    public bool SearchByLastNameOnly; 
} 
public user[] GetUserData(user usr) 
{ 
    //search for users using passed data and return an array of users. 
} 
不必了 2024-07-16 12:02:47

单独传递它们,因为:

  • 这是类型安全的方式。
  • IntelliSense 会在 Visual Studio 中拾取它,当您编写调用函数时,您就会知道是什么。
  • 这样执行起来会更快。

但是,如果参数确实是数组,则传递该数组。 示例:

对于如下所示的函数,请使用以下表示法:

Array FireEmployee(string first, string middle, string last, int id) {...}

对于如下所示的函数,请使用数组:

Array FireEmployees(Employee[] unionWorkers) {...}

Pass them individually, because:

  • that is the type-safe way.
  • IntelliSense will pick it up in Visual Studio and when you write your calling functions, you will know what's what.
  • It is faster to execute that way.

If the parameter really IS the array, though, then pass the array. Example:

For functions which look like this, use this notation:

Array FireEmployee(string first, string middle, string last, int id) {...}

For functions that look like this, use the array:

Array FireEmployees(Employee[] unionWorkers) {...}
享受孤独 2024-07-16 12:02:47

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.

感情废物 2024-07-16 12:02:47

关于 Skeets 对我上面的例子的评论,他会使用类而不是结构,并且可能会更清楚在哪里使用类以及在哪里使用结构,我也发布了这个。 我认为还有其他人也对此感到好奇。

正如我所见,使用类的主要原因是您可以使其不可变,但这对于结构也可能吗?

例如:

struct user 
{ 
public user(string Username, string LastName) 
{ 
    _username = Username; 
} 
private string _username; 
public string UserName { 
    get { return _username; } 
} 

}

我很长时间以来都觉得我不再知道类和结构之间的区别了,因为我们现在可以拥有属性、初始化器、字段以及类在结构中拥有的所有内容。 我知道类是引用类型,结构是值类型,但是在上面的情况下将其用作函数中的参数时有什么区别?

我在网站 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:

struct user 
{ 
public user(string Username, string LastName) 
{ 
    _username = Username; 
} 
private string _username; 
public string UserName { 
    get { return _username; } 
} 

}

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:

Structures can be defined as a tool
for handling a group of logically
related data items. They are
user-defined and provide a method for
packing together data of different
types. Structures are very similar to
Classes. Like Classes, they too can
contain members such as fields and
methods. The main difference between
classes and structures is, classes are
reference types and structures are
value types. In practical terms,
structures are used for smaller
lightweight objects that do not
persist for long and classes are used
for larger objects that are expected
to exist in memory for long periods.

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.

傾旎 2024-07-16 12:02:47

我假设您使用的是 C# 4 并且可以只使用命名参数:

FireEmployee(
    first: "Frank",
    middle: "",
    last: "Krueger",
    id: 338);

这使得代码几乎与 VB 或 Smalltalk 一样可读。 :-)

如果没有,我会同意戴夫·马克尔所说的。

I assume you're using C# 4 and can just use named parameters:

FireEmployee(
    first: "Frank",
    middle: "",
    last: "Krueger",
    id: 338);

These make the code almost as readable as VB or Smalltalk. :-)

If not, I would go with what Dave Markle has to say.

流绪微梦 2024-07-16 12:02:47

如果这是会被大量使用的库代码,并且如果某些参数具有作为默认值候选的典型值,那么您应该考虑 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.

[旋木] 2024-07-16 12:02:47

如果您确实不想单独传递参数,我建议创建一个新类来封装所有参数。 为此,您可以(在 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.

川水往事 2024-07-16 12:02:47

我想说也单独传递它们。 我不喜欢创建一个类,然后将该类作为参数传递的想法。 它是一种邮票耦合形式,这意味着更改将变得更加困难,因为一个类使用另一个类。 重用一个类意味着您也必须重用另一个类。

您可以使用接口来减少标记耦合,但这对我来说开销太大,所以这就是为什么我喜欢单独传递参数。

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.

梦罢 2024-07-16 12:02:47

单个函数真的需要 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.

梦回旧景 2024-07-16 12:02:47

除非函数作用于数组,否则不要将它们作为数组传递,出于以下原因,我不会创建新的数据结构来对参数进行分组

  1. 传递新的数据结构隐藏了函数真正需要的输入(它需要所有数据结构/部分数据吗?)

  2. 与 1 有关它使 UT 变得更加困难(当编写 UT 时,您需要重新创建整个数据结构)

  3. 如果输入参数不相关,您最终会得到一个新的数据结构,该数据结构将不相关的数据类型分组,只是为了使函数调用看起来更整洁

  4. 如果您选择将新数据结构传递给函数,则该函数不能在定义新数据结构的范围内使用

实际上,将每个参数传递给函数的唯一缺点是您可能无法将该函数放入一行代码,但不要忘记在函数调用之前需要的行,您将在其中填充数据结构。

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

  1. Passing a new data structure hides what the function really needs as input (does it need all the data structure/part of it?)

  2. Related to 1 it makes UTs more difficult (when writing a UT you need to recreate the entire data structure)

  3. 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

  4. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文