在什么情况下静态方法是一个好习惯? [英] In what situations is static method a good practice?

查看:158
本文介绍了在什么情况下静态方法是一个好习惯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读以下讨论:

如果私有助手方法可以是静态的,那么它们应该是静态的吗?和
应该如果它们的类没有成员变量,那么所有方法都是静态的。

Should private helper methods be static if they can be static , and
Should all methods be static if their class has no member variables

似乎人们通常会接受静态方法,但对它有点怀疑,由于以下两个原因:

It seems that people in general would accept static methods, but are a little bit skeptical about it, for the following 2 reasons:


  1. 他们很难测试。

  2. 他们违反OO原则。 (他们
    是函数,而不是方法,说一个
    的人。)

最可接受的静态方法私有静态。但那么为什么存在静态方法,在什么情况下它们是首先被采用的?

And the most acceptable static methods are private static ones. But then why do static methods exist at all, and in what situations they are the first priority to be adopted?

推荐答案

静态方法并不难测试。问题是其他代码调用静态方法很难测试,因为你无法替换静态方法。

Static methods aren't hard to test in and of themselves. The problem is that other code calling the static method is hard to test, because you can't replace the static methods.

我认为静态当它们是私有时它们是实用方法 - 例如,方法很好做字符串转义。当您希望能够在测试中模拟或替换的东西使用静态方法时,问题就出现了。工厂方法也可以有用,虽然依赖注入通常是一种更好的方法 - 再次,它部分取决于您是否希望能够替换测试中的功能。

I think static methods are fine either when they're private or when they're "utility" methods - e.g. to do string escaping. The problem comes when you use static methods for things that you want to be able to mock out or otherwise replace within tests. Factory methods can be useful too, although dependency injection is generally a better approach - again, it partly depends on whether you want to be able to replace the functionality in tests.

至于不是OO - 不是你用一般OO语言写的所有内容都必须是纯粹的OO。有时非OO路线更加务实,导致代码更简单。 Eric Lippert有一篇很棒的博客文章,遗憾的是我现在找不到。但是,这篇文章是相关的。它讨论的是扩展方法而不是静态方法,但原理是相同的。

As for not being "OO" - not everything you write in a generally OO language has to be "pure" OO. Sometimes the non-OO route is simply more pragmatic and leads to simpler code. Eric Lippert has a great blog post about this, which unfortunately I can't find right now. However, there's a comment in this post which is relevant. It talks about extension methods rather than static methods, but the principle is the same.


扩展方法经常被批评为
不够OOP。这对我来说似乎是
将马车放在
的前面。 OOP的目的是为
提供结构
大型软件项目的指导方针
团队的人不需要
知道每个$ b $的内部细节b其他人的工作是为了
富有成效。 C#的目的是
一种有用的编程语言,
使我们的客户能够在我们的平台上实现高效的
。很明显,OOP是
有用且受欢迎,因此我们只需要
就可以在C#中以OOP风格轻松实现
程序。但是C#的
目的不是成为OOP
语言。我们评估基于
的功能是否对我们的
客户有用,而不是基于他们是否b $ b严格符合某些抽象
学术理想的b $ b b语言面向对象。我们
愉快地从oo,
功能,程序,命令,
声明,等等中获取想法,只要我们
可以制作一致,有用的产品
这对我们的客户有利。

Extension methods are often criticized as being "not OOP enough". This seems to me to be putting the cart in front of the horse. The purpose of OOP is to provide guidelines for the structuring of large software projects written by teams of people who do not need to know the internal details of each other's work in order to be productive. The purpose of C# is to be a useful programming language that enables our customers to be productive on our platforms. Clearly OOP is both useful and popular, and we've therefore tried to make it easy to program in an OOP style in C#. But the purpose of C# is not "to be an OOP language". We evaluate features based on whether they are useful to our customers, not based on whether they conform strictly to some abstract academic ideal of what makes a language object-oriented. We'll happily take ideas from oo, functional, procedural, imperative, declarative, whatever, so long as we can make a consistent, useful product that benefits our customers.

这篇关于在什么情况下静态方法是一个好习惯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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