What is the general thinking on the use of extension methods that serve no purpose other than enhancing readability?
除了增强可读性之外,使用扩展方法的一般思考是什么?
Without using extension methods we might have the method
不使用扩展方法我们可能有方法
IEnumerable<DependencyObject> GetDescendents(DependencyObject root) {}
that could be called with
可以调用
var descendents = GetDescendents(someControl);
or
foreach (var descendent in GetDescendents(someControl)) {}
Although there's nothing wrong with this I find the instance.method() notation to be more readable so I might consider making this an extension method with this signature
虽然这没有什么问题,但我发现instance.method()表示法更具可读性,所以我可以考虑使用这个签名使其成为扩展方法
public IEnumerable<DependencyObject> GetDescendents(this DependencyObject root) {}
allowing it to be called with
允许它被调用
var descendents = someControl.GetDescendents();
or
foreach (var descendent in someControl.GetDescendents()) {}
So my question is whether you think this is reasonable or an abuse of extension methods. If it was simply a matter of declaring the function differently I wouldn't hesitate; but the fact that using an extension method requires it be coded in a different, static class makes me wonder if it's worth the effort or not. The example I'm using above is a fairly generic one and might have merit as an extension method that will be used in several places but often this is not the case and I would be coding the static class containing the extension in the same file as the single class that uses it.
所以我的问题是你认为这是合理的还是滥用扩展方法。如果仅仅是以不同的方式宣布功能,我会毫不犹豫;但是使用扩展方法要求它在不同的静态类中编码这一事实让我想知道它是否值得付出努力。我上面使用的示例是一个相当通用的示例,并且可能具有作为将在若干地方使用的扩展方法的优点,但通常情况并非如此,并且我将在同一文件中编写包含扩展名的静态类。使用它的单个类。
3 个解决方案
#1
I think the big advantage of extension methods is discoverability. If someone is unaware that one of their team members created a GetDescendents method in a utility class somewhere, they'll never use it. However, if that method starts to show up in Intellisense or in the Object Browser, there's a decent chance they will stumble across it. If you start to make more extensive use of extension methods, people will start to use the tools I mentioned to look for extensions that add value.
我认为扩展方法的最大优点是可发现性。如果某人不知道他们的某个团队成员在某个实用程序类中创建了一个GetDescendents方法,他们就永远不会使用它。但是,如果该方法开始出现在Intellisense或对象浏览器中,那么他们就有可能偶然发现它。如果您开始更广泛地使用扩展方法,人们将开始使用我提到的工具来寻找增值的扩展。
#2
Most if not all extension methods fall into this category to some degree, since they can't operate on the internals of a class anymore than your static function. At any rate, any extension method can be rewritten in a static class with an extra parameter representing the object (arguably, that's exactly what an extension method is anyway).
大多数(如果不是全部)扩展方法在某种程度上属于这个类别,因为它们不能在静态函数上对类的内部进行操作。无论如何,任何扩展方法都可以在静态类中重写,并使用一个额外的参数来表示对象(可以说,这正是扩展方法无论如何)。
To me, it's entirely a question of style: in the example you provided, I'd probably jump for the extension method. I think the important question here is, Is this function something I'd write as part of the class if I were to reimplement the class, and does it make sense as such? If yes, then go for it, if no, then consider a different solution.
对我而言,这完全是一个风格问题:在你提供的例子中,我可能会跳转到扩展方法。我认为这里的重要问题是,如果我要重新实现这个类,那么这个函数是否是我作为课程的一部分编写的,它是否有意义?如果是,那就去吧,如果没有,那就考虑一个不同的解决方案。
#3
An extension method only exists to improve readability - it is merely a syntax shortcut that allows you, by specifying the this
keyword on the first argument, allows you to call the method on the object instance in question. So I think it is entirely reasonable.
扩展方法仅用于提高可读性 - 它只是一种语法快捷方式,允许您通过在第一个参数上指定this关键字,允许您在相关对象实例上调用方法。所以我认为这是完全合理的。
#1
I think the big advantage of extension methods is discoverability. If someone is unaware that one of their team members created a GetDescendents method in a utility class somewhere, they'll never use it. However, if that method starts to show up in Intellisense or in the Object Browser, there's a decent chance they will stumble across it. If you start to make more extensive use of extension methods, people will start to use the tools I mentioned to look for extensions that add value.
我认为扩展方法的最大优点是可发现性。如果某人不知道他们的某个团队成员在某个实用程序类中创建了一个GetDescendents方法,他们就永远不会使用它。但是,如果该方法开始出现在Intellisense或对象浏览器中,那么他们就有可能偶然发现它。如果您开始更广泛地使用扩展方法,人们将开始使用我提到的工具来寻找增值的扩展。
#2
Most if not all extension methods fall into this category to some degree, since they can't operate on the internals of a class anymore than your static function. At any rate, any extension method can be rewritten in a static class with an extra parameter representing the object (arguably, that's exactly what an extension method is anyway).
大多数(如果不是全部)扩展方法在某种程度上属于这个类别,因为它们不能在静态函数上对类的内部进行操作。无论如何,任何扩展方法都可以在静态类中重写,并使用一个额外的参数来表示对象(可以说,这正是扩展方法无论如何)。
To me, it's entirely a question of style: in the example you provided, I'd probably jump for the extension method. I think the important question here is, Is this function something I'd write as part of the class if I were to reimplement the class, and does it make sense as such? If yes, then go for it, if no, then consider a different solution.
对我而言,这完全是一个风格问题:在你提供的例子中,我可能会跳转到扩展方法。我认为这里的重要问题是,如果我要重新实现这个类,那么这个函数是否是我作为课程的一部分编写的,它是否有意义?如果是,那就去吧,如果没有,那就考虑一个不同的解决方案。
#3
An extension method only exists to improve readability - it is merely a syntax shortcut that allows you, by specifying the this
keyword on the first argument, allows you to call the method on the object instance in question. So I think it is entirely reasonable.
扩展方法仅用于提高可读性 - 它只是一种语法快捷方式,允许您通过在第一个参数上指定this关键字,允许您在相关对象实例上调用方法。所以我认为这是完全合理的。