简体   繁体   English

是否应该在构造函数签名中列出可选参数?

[英]Should optional parameters be listed in a constructor signature?

With best practices in mind, what's the advisable approach to adding optional parameters to a constructor signature? 考虑到最佳实践,将可选参数添加到构造函数签名的明智方法是什么? Should you only ever list the core parameters and depend on initialisers for the non optional properties? 您是否应该只列出核心参数并依赖于初始化程序的非可选属性? Seems to me to be the most sensible approach! 在我看来是最明智的方法!

In particular with the ability to assign property values with object initializers, multiple overloads for constructors are far less useful than they were. 特别是通过使用对象初始化程序分配属性值的能力,构造函数的多个重载远没有以前有用。 They were mostly useful in the past for setting mutable properties for code compactness. 过去,它们对于设置可变性以实现代码紧凑性非常有用。

Now one can write 现在可以写

MyClass my = new MyClass() { PropA = 1, PropB = 2 };

Almost as compact as 几乎和

MyClass my = new MyClass(1, 2);

while being more expressive. 同时更具表现力。

However, sometimes the nature of an object dictates that an overload would provide a meaningful contract to the object consumer. 但是,有时对象的性质决定了过载将为对象使用者提供有意义的契约。 For example, providing an overload for a Rectangle class that accepts a length and a width is not unreasonable (I would not ding it on a code review). 例如,为接受长度和宽度的Rectangle类提供重载并不是没有道理的(我不会在代码审查中看到它)。 Personally I would still not provide an overload because object initializer syntax is more expressive, avoiding questions like "was that Rectangle(int length, int width) or Rectangle(int width, int length)?" 就个人而言,我仍然不会提供重载,因为对象初始化程序的语法更具表现力,避免了诸如“是Rectangle(int length,int width)还是Rectangle(int width,int length)?”之类的问题。 while reading code (I know, intellisense helps while writing code). 在阅读代码时(我知道,在编写代码时intellisense会有所帮助)。

I believe this is a fine approach. 我相信这是个好方法。 Typically people use properties for optional parameters; 人们通常使用属性作为可选参数。 but this makes the object mutable. 但这使对象可变。 Using optional constructor arguments/ctor overloads is a good approach to keep the object immutable. 使用可选的构造函数参数/ ctor重载是使对象保持不变的好方法。

As a rule of thumb, I add to a constructor only the parameters that are required to create a valid, working object. 根据经验,我只向构造函数添加创建有效的工作对象所需的参数。 With the advent of object initializer syntax, it is much easier for the client to configure whatever other parameters he wants. 随着对象初始化程序语法的出现,客户端可以更轻松地配置他想要的任何其他参数。

However, I think that this rule should have many exceptions: many times you might want to provide an overload that makes it easy and clearer to the developer what he can expect to be able to change about an object. 但是,我认为该规则应该有很多例外:很多时候,您可能希望提供一个重载,从而使开发人员更容易,更清楚地了解到他可以期望对某个对象进行更改的内容。

Consider subclassing instead of several constructors. 考虑使用子类而不是几个构造函数。

Reason: Often two different different constructors indicate two different behaviors are desired. 原因:通常,两个不同的构造函数表示需要两种不同的行为。 Different nehaviors should be implemented by different classes. 不同的行为应该由不同的类来实现。

The subclass would then have both a name that indicates how it's a specialized version of the base class, and a constructor that ensures that this kind of object can only be created in a valid state. 然后,该子类将具有一个名称,该名称指示它是基类的一种特殊版本,以及一个构造函数,该构造函数确保只能在有效状态下创建此类对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM