简体   繁体   English

更改参数名称而不破坏向后兼容性

[英]Change parameter name without breaking backwards compatibility

I am working on ac# library, so we are concerned with breaking backwards compatibility, but I was wondering is it possible to change just the name of a parameter and maintain backwards compatibility because of the ability to use named parameters? 我正在研究ac#library,所以我们关注的是打破向后兼容性,但我想知道是否可以更改参数的名称并保持向后兼容性,因为能够使用命名参数? An example of what I am trying to do is below 我想要做的一个例子如下

[Obsolete("use ChangeSpecificFoo(SpecificFoo specificFoo)")]
public void ChangeSpecificFoo(SpecificFoo foo)
{
    _specificFoo = foo;
}

//Compile error ... already defines a member called 'ChangeSpecificFoo' with the same parameter types
public void ChangeSpecificFoo(SpecificFoo specificFoo)
{
    _specificFoo = specificFoo;
}

Just changing the parameter name runs the potential risk of breaking backwards compatibility because someone could be calling the method using named parameters like ChangeSpecificFoo(foo: someSpecificFoo) , but we can't deprecate the method by adding a new method with the correct parameter name because parameter names are not included in the method signature, so the compiler sees it as a duplicate. 只是更改参数名称会产生破坏向后兼容性的潜在风险,因为有人可能使用命名参数(如ChangeSpecificFoo(foo: someSpecificFoo)调用方法,但我们不能通过添加具有正确参数名称的新方法来弃用该方法,因为参数名称不包含在方法签名中,因此编译器将其视为重复。

Is there any way around this? 有没有办法解决? The only alternatives I see are changing the method name so it is not a duplicate and then deprecating the old method, or waiting until we add or remove parameters from the parameter list and changing the parameter names then(this may never happen because the method is pretty stable), or just make the change and fix any breaks that we may have from code using this library as we find them. 我看到的唯一选择是更改方法名称,因此它不是重复,然后弃用旧方法,或等到我们从参数列表中添加或删除参数并更改参数名称(这可能永远不会发生,因为方法是非常稳定),或者只是进行更改并修复我们在找到它们时使用此库从代码中获得的任何中断。

My first inclination for this is simple: DON'T . 我对此的第一个倾向很简单: 不要 The name of your parameter is irrelevant outside of the method body. 参数的名称在方法体外部无关紧要。 You're right to consider people calling it out by name, and therefore potentially breaking it. 你认为人们通过名字来呼唤它是正确的,因此可能会破坏它。 However, just changing the name of the parameter gives no real benefit. 但是,仅更改参数名称不会带来任何实际好处。

The only possible reason for changing the name is to redefine what the method does because the old name leads to confusion. 更改名称的唯一可能原因是重新定义该方法的作用,因为旧名称会导致混淆。 In that case, the name of the method should also be changed in order to not introduce another form of confusion. 在这种情况下,还应该更改方法的名称,以免引入另一种形式的混淆。 (The fact that the method signatures are identical is the first and more important reason to not change parameter names. However, this is to potentially explain why you might want to.) (方法签名相同的事实是不更改参数名称的第一个也是更重要的原因。但是,这可能会解释您可能想要的原因 。)

If however, you are still adamant about keeping the same method signature, but altering the name, you could do this. 但是,如果您仍然坚持保持相同的方法签名,但更改名称,则可以执行此操作。 (Again, I'm strongly recommending you either don't change it at all, or rename the method as well to continue to eliminate confusion.) (同样,我强烈建议你要么根本不改变它,要么重命名方法以继续消除混淆。)

One way around this would be to have the method with both parameters, but make the second optional. 解决这个问题的一种方法是使用两个参数的方法,但使第二个可选。 Have that last parameter use the old name, and then assign it over within the method. 让最后一个参数使用旧名称,然后在方法中分配它。

I would also HIGHLY recommend logging any uses of the named parameter, to see if your concern is valid about people calling it as a named parameter. 我还强烈建议记录命名参数的任何使用,以查看您的关注对于将其作为命名参数调用的人是否有效。

public void ChangeSpecificFoo(SpecificFoo specificFoo = null, SpecificFoo foo = null)
{
    if (foo != null && specificFoo == null)
    {
        // Add any other details you can, especially 
        // to figure out who is calling this.
        Log("Someone used a name parameter!!");
    }
    _specificFoo = specificFoo ?? foo;
}

As Dmitry Bychenko pointed out in the comments, this will not stop anyone from calling this method like so: ChangeSpecificFoo(null, new SpecificFoo()) , which will trigger a logging. 正如Dmitry Bychenko在评论中指出的那样,这不会阻止任何人像这样调用这个方法: ChangeSpecificFoo(null, new SpecificFoo()) ,它将触发日志记录。

His observation introduces another reason why this is a bad idea: You're now introducing ANOTHER way for people to "incorrectly" call your method. 他的观察引入了另一个原因,为什么这是一个坏主意:你现在正在引入另一种方式让人们“错误地”调用你的方法。 Therefore, I'll repeat my advice from the top of my answer: DON'T do this, unless you really really really need to change that parameter name. 因此,我将从我的答案的顶部重复我的建议: 要这样做,除非你真的真的需要更改参数名称。

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

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