可选参数真的是可取的吗? [英] Are optional parameters really an advisable thing to use ?

查看:55
本文介绍了可选参数真的是可取的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



大家好,


过去我在

函数中使用了几个时间可选参数。 />

但现在我更倾向于认为它们比

更有用,而且最好还是可以避免。


我想听听你对此事的各种意见。


谢谢,


-P


Hi guys,

In the past I have used several time optional parameters in my
function.

But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.

I''d like to hear your various opinions on this matter.

Thanks,

-P

推荐答案



< pa *********** @ libero.itwrote in message

新闻:11 ********************** @ b28g2000cwb.googlegr oups.com ...

<pa***********@libero.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...

>

大家好,


过去我在
$ b中使用了几个时间可选参数$ b函数。


但现在我更倾向于认为它们比

更有用,并且可能更好地避免使用。


我想听听你对此事的各种意见。


Th anks,

-P
>
Hi guys,

In the past I have used several time optional parameters in my
function.

But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.

I''d like to hear your various opinions on this matter.

Thanks,

-P



我个人从未使用过它们。我通常使用不同的参数数量(每个参数一个)实现方法,这些方法调用广义参数。方法

使用合适的默认值获取所有参数。我发现这是

更容易扩展。编写的代码更多,但是更少的潜在危险/混乱我认为仍然,它们在使用Office Interop和COM处理

时特别有用我必须说。


Public Sub Foo(ByVal变量为整数)


Foo(变量,-1,-1)
< br $>
End Sub


Public Sub Foo(ByVal Variable1 As Integer,ByVal Variable2 As Integer)


Foo(Variable1 ,Variable2,-1)


End Sub


Public Sub Foo(ByVal Variable1 As Integer,ByVal Variable2 As Integer,

ByVal Variable3 As Integer)


.......


End Sub

Personally I''ve never used them. I usually implement methods with varying
numbers of parameters (one for each), that call into a "generalized" method
that takes all of the parameters, using suitable defaults. I find this is
easier to extend. It''s more code to write, but less potential for
hazards/confusion I think Still, they are particularly useful when dealing
with Office Interop and COM I have to say.

Public Sub Foo ( ByVal Variable As Integer )

Foo ( Variable, -1, -1 )

End Sub

Public Sub Foo ( ByVal Variable1 As Integer, ByVal Variable2 As Integer )

Foo ( Variable1, Variable2, -1 )

End Sub

Public Sub Foo ( ByVal Variable1 As Integer, ByVal Variable2 As Integer,
ByVal Variable3 As Integer )

.......

End Sub


< pa *********** @ libero.itschrieb:
<pa***********@libero.itschrieb:

过去我用了好几次我的

函数中的可选参数。


但现在我更倾向于认为它们比

更有用,可能最好避免。
In the past I have used several time optional parameters in my
function.

But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.



我想知道你是如何认识到可选参数是危险的。$ / b

我个人使用可选参数,只要它有意义。在另一个

手上,当它比可选参数更有意义时我使用重载。

一旦库具有
,任选参数''默认值不得更改
已经发布(好吧,方法签名也不能改变),他们可以将
视为界面的一部分。


因此可选只有当可选参数''

默认值未来不会改变且默认值不是

实现细节时,才应使用这些参数。如果是后者,使用重载是更好的解决方案。如果是第一种情况,请使用普通参数,例如

示例。


请注意,C#不支持可选参数。具有可选的

参数的方法可以在C#中定义,但它们不能像在VB中完成的那样消耗。总是所有参数都必须在方法调用中指定。


-

MS Herfried K. Wagner

MVP< URL :http://dotnet.mvps.org/>

VB< URL:http://dotnet.mvps.org/dotnet/faqs/>

I wonder how you come to the perception that optional parameters are
dangerous.

Personally I use optional parameters whereever it makes sense. On the other
hand I use overloading when it makes more sense than optional parameters.
Optional parameters'' default values must not be changed once a library has
been published (well, method signatures must not be changed too), they can
be seen as part of the interface.

Thus optional parameters should only be used if the optional parameter''s
default value won''t change in future and if the default value is not an
implementation detail. If the latter is the case, using overloads is the
better solution. If the first is the case, use a normal parameter, for
example.

Note that C# does not support optional parameters. Methods having optional
parameters can be defined in C# but they cannot be consumed the way this is
done in VB. Always all parameters must be specified in the method calls.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


过去我在

函数中使用了几个时间可选参数。

但是现在我更倾向于认为它们比

更有用,而且可能更好避免。

我想听听你对此的各种意见物。
function.
But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.
I''d like to hear your various opinions on this matter.



1. Fxcop说要避免使用规则默认参数不应该使用
。据说,可选参数可以导致互操作性甚至在.net内的问题(VB支持可选参数而C#不支持)。

首选替代方法是重载。那么,这是关于什么的? VB

超支还是C#缺陷?这是.net实现中的一个问题吗?或者b / b $ b $可选参数构成理论上无法解决的问题吗?有趣的是

一般的问题,但与我无关,因为我只使用VB。在

VB唯一的世界中,我不认为可选参数会造成任何已知的技术问题。


2。至于更危险而不是有用的问题,即

可选参数的效用......好吧,我喜欢它们,我经常使用它们。我也使用

重载。我估计我的代码使用可选参数而不是重载

,比例约为80-20。正如您所料,当调用序列非常相似时,我使用可选的

参数,当调用序列非常不同时,我使用重载

。我甚至有两个

重载的情况,每个都有可选参数。我通常编码一个默认值

的可选参数是Nothing(或者对于值类型,",0或类似)

表示类似''不适用''或''什么都不做''。在这些

新闻组中,我最近读了一个例子,其中一个可选参数是
SalesTax,默认值是6(表示6%) - 我从不使用可选的
以这种方式
参数。我确实有一些非零数字可选

参数的情况,例如字符串长度参数默认为最差的b $ b b情况,或乘法缩放因子默认为1(意思是将
乘以一个,因此没有缩放),但这些非零参数与销售税示例完全不同

。我也有几个类实现了一个

构造函数,所有参数都是可选的 - 我发现这是

方便但不是必需的。总之,我赞成明智地使用

可选参数。

1. Fxcop says to avoid them with the rule "Default parameters should not be
used." Optional parameters, it is said, can lead to interoperability
problems even within .net (VB supports optional parameters and C# does not).
The preferred alternative is overloading. So, what is this about? Does VB
overreach or is C# deficient? Is this a problem in .net implementation or do
optional parameters pose theoretically unsolvable problems? Interesting
questions in general, but not relevant to me, because I use VB only. In the
VB only world, I don''t think optional parameters pose any known technical
problems.

2. As to the question of "more dangerous than useful", ie the utility of
optional parameters.... Well, I like them, and I use them often. I also use
overloads. I estimate that my code uses optional parameters versus overloads
in proportions of about 80-20. As you would expect, I use optional
parameters when the calling sequences are very similar, and I use overloads
when the calling sequences are very different. I even have a case of two
overloads, and each has optional parameters. I usually code a default value
of an optional parameter is Nothing (or for value types, "", 0, or similar)
which indicates something like ''not applicable'' or ''do nothing''. In these
newsgroups, I recently read an example where an optional parameter was
SalesTax and the default value was 6 (indicating 6%) - I never use optional
parameters in this way. I do have some cases of a nonzero numerical optional
parameters, for example a string length parameter defaulted to the worst
case, or a multiplicative scaling factor defaulted to one (meaning multiply
by one, hence no scaling), but these nonzero parameters are quite different
from the sales tax example. I also have a few classes that implement one
constructor having all parameters optional - I have found this to be
convenient but not necessary. In short, I am in favor of judicious use of
optional parameters.


这篇关于可选参数真的是可取的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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