SOLID用于功能性编程 [英] SOLID for functional programming

查看:147
本文介绍了SOLID用于功能性编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自OOP语言,我熟悉面向对象设计的SOLID原则。看起来这些中的一些可能适合功能性编程模型,而其他部分在缺乏状态的世界中没有意义。有没有类似的重构功能代码的原则?

解决方案

据我所知(我不是专家),固体原理并没有说明任何关于状态的内容。它们也应该适用于函数式编程语言。他们对如何实现模块化提出了更多建议。



其中一些比较显而易见,或者至少是众所周知的。单一职责是UNIX原则做一件事并做得很好,在组合被广泛使用的功能语言中,这一原则更为流行。接口隔离原理也非常自然(让你的接口模块化并保持正交概念分离)。最后,依赖倒置只是抽象的名称,在函数式编程中是无处不在的。



OL原则Open / Closed和LSP更倾向于基于继承的语言作为核心软件工程概念。功能语言值/模块默认没有开放式递归,所以实现继承仅用于特定的情况。组合物是优选的。我不确定你应该如何解释该设置中的开放/封闭原则。你可能会认为它是关于封装的,哪些函数式程序也使用了很多,使用抽象类型等。

最后,Liskov替换原则看起来可能与继承有关。函数式语言并不总是使用子类型,但是当它们这样做时,确实假定派生类型应该保留基本类型的规范。函数式程序员当然要小心地指定和尊重他们的程序,模块等的接口和属性,并且可以在编程,重构等基础上根据这些规范使用代数推理(这相当于这个,所以我可以替代...)等等。然而,一旦你摆脱了默认继承的想法,你的接口违规问题就会少得多,所以LSP并没有被强调为OOP中的重要保障。


Coming from an OOP language, I am familiar with the SOLID principles of object oriented design. It seems like some of these would fit into a functional programming model, while other parts make no sense in a world lacking state. Is there a similar set of principles for refactoring functional code?

解决方案

As far as I know (I'm no expert), SOLID principles do not tell anything about state. They should be applicable as well in a functional programming languages. They're more advice about how to achieve modularity.

Some of them are rather obvious or at least well-known. Single-responsibility is the UNIX principle "do one thing and do it well", which is even more popular in functional languages where "composition" is widely used, similarly. The Interface Segregation Principle is very natural as well (have your interfaces modular and keep orthogonal concepts separated). Finally, Dependency Inversion is just a name for "abstraction" and is omnipresent in functional programming.

The "OL" principles, Open/Closed and LSP, are more oriented towards languages based upon inheritance as a core software engineering concept. Functional languages values/modules do not have open recursion by default, so "implementation inheritance" is only used in very specific cases. Composition is preferred. I'm not sure how you should interpret the Open/Closed principle in that setting. You may consider it is about encapsulation, which functional programs also use a lot, using abstract types and such.

Finally, the Liskov Substitution Principle may seem to be about inheritance. Functional languages do not always use subtyping, but when they do it is indeed assumed that "derived types" should preserve the specification of "base types". Functional programmers are of course careful to specify and respect the interface and properties of their programs, modules etc., and may use algebraic reasoning (this is equivalent to this so I can substitute...) based on those specifications when programming, refactoring, etc. However, once you get rid of the "inheritance by default" idea, you have much less problems of interface violations, so LSP is not emphasized as a vital safeguard as it is in OOP.

这篇关于SOLID用于功能性编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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