IntelliTect / IntelliTect/CodingGuidelines

Flag optional arguments on methods that differ from their interface declarations

未关闭
#102 5 条评论 0 个 reaction 已指派 4 人 已被 @GrantErickson 认领 在 GitHub 查看
documentation
主要语言
C#
星标
12
派生
16
平均合并
2 分钟
30 天内合并 PR
7

描述

Consider the following case:
```C#
public class Foo : IFoo
{
public void Bar(int value = 2) => Console.WriteLine(value);
}
public interface IFoo
{
public void Bar(int value = 1);
}

public static void Main(string[] args)
{
IFoo foo1 = new Foo();
foo1.Bar();
var foo2 = new Foo();
foo2.Bar();
}
```
Output:
```
2
1
```

I would propose that the coding standard be
```
DO NOT: declare optional arguments on class methods that are implementing an interface.
```
Rational:
- If these value are not in sync it will cause behavior as shown above.
- It is possible to declare a constant and share it in both places. However, if you have an interface for accessing the class, why is there code bypassing it? If you want to invoke the method using the default value, simply go through the interface.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。