我应该更喜欢C#中的静态方法吗?

时间:2021-10-30 05:30:29

Having spent a bit of time learning about functional programming, it's becoming more and more natural for me to want to work with static methods that don't perform any mutation.

花了一些时间学习函数式编程,我想要使用不执行任何变异的静态方法变得越来越自然。

Are there any reasons why I should curb this instinct?

有什么理由可以抑制这种本能吗?

6 个解决方案

#1


19  

The question I find a bit odd, because static methods and methods that perform no mutations are two orthogonal classifications of methods. You can have mutating static methods and nonmutating instance methods.

我发现这个问题有点奇怪,因为不执行突变的静态方法和方法是两种正交的方法分类。您可以使用变异静态方法和非突变实例方法。

For me, it has become more and more natural to combine functional and oo programming; I like instance methods that perform no mutations. Functional programming is easier to understand because it discourages complex mutations; OO programming is easier to understand because the code and the data it operates on are close together. Why choose? Embrace the power of "and"; do both!

对我来说,结合功能和oo编程变得越来越自然;我喜欢不执行突变的实例方法。函数式编程更容易理解,因为它阻止了复杂的突变; OO编程更容易理解,因为它运行的代码和数据非常接近。为什么选择?拥抱“和”的力量;两个都做!

#2


12  

You can write working programs this way, but it's not idiomatic. If you want to work on a team, I'd try to curb it. If no one else is reading your code, go nuts.

你可以用这种方式编写工作程序,但这不是惯用语。如果你想在团队中工作,我会试着遏制它。如果没有其他人在阅读您的代码,请坚持下去。

#3


5  

For some reason I think of this quote when I read your question:

出于某种原因,当我读到你的问题时,我会想到这句话:

You can write Fortran in any language.

你可以用任何语言编写Fortran。

If the intent of C# were to be purely functional, static would be unnecessary because everything would be static by default. If you are strict about following OOP practices and the SOLID principles, your code effectively becomes functional (I know there's a quote out there about this somewhere) so you end up getting the best of both worlds.

如果C#的意图纯粹是功能性的,那么静态将是不必要的,因为默认情况下一切都是静态的。如果你严格遵守OOP实践和SOLID原则,那么你的代码就会有效地发挥作用(我知道这里有一个关于这个问题的引用)所以你最终会得到两全其美的结果。

The reason I would curb it in a multi-user project would be that it's not typical C# (it's really C# with handcuffs). You just need one person to break the rule and declare a static mutable property and everything goes to hell.

我在多用户项目中遏制它的原因是它不是典型的C#(它真的是带有手铐的C#)。你只需要一个人来打破规则并声明一个静态的可变属性,一切都会变成地狱。

#4


0  

Not completely. I do like my extension methods, and Linq, but an OO-language should be used in an OO fashion. Besides, it's all imperative at the CPU, and for several layers on top of that.

不完全的。我喜欢我的扩展方法和Linq,但OO语言应该以OO方式使用。此外,它在CPU上都是必不可少的,而且最重要的是几层。

#5


0  

Great question.

好问题。

I think the answer depends on the context of what your code does, and how much of it is static.

我认为答案取决于你的代码所做的上下文,以及它有多少是静态的。

My code has been seeing less static cases since I program to interfaces a lot now and mark some methods 'protected virtual' rather than 'static' for cases such as unit testing's extract and override pattern. Thats not to say you couldnt just call static methods from those, though.

我的代码看到的静态情况较少,因为我现在编程接口很多并且标记了一些方法'protected virtual'而不是'static'用于单元测试的提取和覆盖模式等情况。多数民众赞成不是说你不能从这些方法中调用静态方法。

#6


-2  

There is many reasons should curb this instinct when you write OOP.

当你编写OOP时,有很多理由应该遏制这种本能。

Object states and behaviours;

对象状态和行为;

Static class and method need parameters, and you should be sure which paramters will pass to static methods. But class manages itself if method related with is state.

静态类和方法需要参数,您应该确定哪些参数将传递给静态方法。但是如果相关的方法是状态,则类管理自己。

I think that only this reason enough to curb static modifiers.

我认为只有这个理由足以抑制静态修饰符。

And for clear reason, we should listen Mrs. Liskov. here

出于明确的原因,我们应该听Liskov夫人。这里

#1


19  

The question I find a bit odd, because static methods and methods that perform no mutations are two orthogonal classifications of methods. You can have mutating static methods and nonmutating instance methods.

我发现这个问题有点奇怪,因为不执行突变的静态方法和方法是两种正交的方法分类。您可以使用变异静态方法和非突变实例方法。

For me, it has become more and more natural to combine functional and oo programming; I like instance methods that perform no mutations. Functional programming is easier to understand because it discourages complex mutations; OO programming is easier to understand because the code and the data it operates on are close together. Why choose? Embrace the power of "and"; do both!

对我来说,结合功能和oo编程变得越来越自然;我喜欢不执行突变的实例方法。函数式编程更容易理解,因为它阻止了复杂的突变; OO编程更容易理解,因为它运行的代码和数据非常接近。为什么选择?拥抱“和”的力量;两个都做!

#2


12  

You can write working programs this way, but it's not idiomatic. If you want to work on a team, I'd try to curb it. If no one else is reading your code, go nuts.

你可以用这种方式编写工作程序,但这不是惯用语。如果你想在团队中工作,我会试着遏制它。如果没有其他人在阅读您的代码,请坚持下去。

#3


5  

For some reason I think of this quote when I read your question:

出于某种原因,当我读到你的问题时,我会想到这句话:

You can write Fortran in any language.

你可以用任何语言编写Fortran。

If the intent of C# were to be purely functional, static would be unnecessary because everything would be static by default. If you are strict about following OOP practices and the SOLID principles, your code effectively becomes functional (I know there's a quote out there about this somewhere) so you end up getting the best of both worlds.

如果C#的意图纯粹是功能性的,那么静态将是不必要的,因为默认情况下一切都是静态的。如果你严格遵守OOP实践和SOLID原则,那么你的代码就会有效地发挥作用(我知道这里有一个关于这个问题的引用)所以你最终会得到两全其美的结果。

The reason I would curb it in a multi-user project would be that it's not typical C# (it's really C# with handcuffs). You just need one person to break the rule and declare a static mutable property and everything goes to hell.

我在多用户项目中遏制它的原因是它不是典型的C#(它真的是带有手铐的C#)。你只需要一个人来打破规则并声明一个静态的可变属性,一切都会变成地狱。

#4


0  

Not completely. I do like my extension methods, and Linq, but an OO-language should be used in an OO fashion. Besides, it's all imperative at the CPU, and for several layers on top of that.

不完全的。我喜欢我的扩展方法和Linq,但OO语言应该以OO方式使用。此外,它在CPU上都是必不可少的,而且最重要的是几层。

#5


0  

Great question.

好问题。

I think the answer depends on the context of what your code does, and how much of it is static.

我认为答案取决于你的代码所做的上下文,以及它有多少是静态的。

My code has been seeing less static cases since I program to interfaces a lot now and mark some methods 'protected virtual' rather than 'static' for cases such as unit testing's extract and override pattern. Thats not to say you couldnt just call static methods from those, though.

我的代码看到的静态情况较少,因为我现在编程接口很多并且标记了一些方法'protected virtual'而不是'static'用于单元测试的提取和覆盖模式等情况。多数民众赞成不是说你不能从这些方法中调用静态方法。

#6


-2  

There is many reasons should curb this instinct when you write OOP.

当你编写OOP时,有很多理由应该遏制这种本能。

Object states and behaviours;

对象状态和行为;

Static class and method need parameters, and you should be sure which paramters will pass to static methods. But class manages itself if method related with is state.

静态类和方法需要参数,您应该确定哪些参数将传递给静态方法。但是如果相关的方法是状态,则类管理自己。

I think that only this reason enough to curb static modifiers.

我认为只有这个理由足以抑制静态修饰符。

And for clear reason, we should listen Mrs. Liskov. here

出于明确的原因,我们应该听Liskov夫人。这里