OOP vs 函数式编程 vs 过程式编程 [英] OOP vs Functional Programming vs Procedural

查看:24
本文介绍了OOP vs 函数式编程 vs 过程式编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些编程范式之间有什么区别,它们是更适合特定问题还是有任何用例偏向于其他?

What are the differences between these programming paradigms, and are they better suited to particular problems or do any use-cases favour one over the others?

感谢架构示例!

推荐答案

他们各有各的好 - 他们只是针对相同问题的不同方法.

All of them are good in their own ways - They're simply different approaches to the same problems.

在纯粹的程序风格中,数据往往与对其进行操作的函数高度分离.

In a purely procedural style, data tends to be highly decoupled from the functions that operate on it.

在面向对象的风格中,数据往往带有一组函数.

In an object oriented style, data tends to carry with it a collection of functions.

在函数式风格中,数据和函数往往有更多的共同点(如在 Lisp 和 Scheme 中),同时在函数的实际使用方式方面提供更多的灵活性.算法也倾向于根据递归和组合而不是循环和迭代来定义.

In a functional style, data and functions tend toward having more in common with each other (as in Lisp and Scheme) while offering more flexibility in terms of how functions are actually used. Algorithms tend also to be defined in terms of recursion and composition rather than loops and iteration.

当然,语言本身只影响首选哪种风格.即使在像 Haskell 这样的纯函数式语言中,您也可以以过程风格编写(尽管非常不鼓励这样做),甚至在像 C 这样的过程语言中,您也可以以面向对象的风格进行编程(例如在 GTK+ 和EFL API).

Of course, the language itself only influences which style is preferred. Even in a pure-functional language like Haskell, you can write in a procedural style (though that is highly discouraged), and even in a procedural language like C, you can program in an object-oriented style (such as in the GTK+ and EFL APIs).

需要明确的是,每种范式的优势"仅在于您的算法和数据结构的建模.例如,如果您的算法涉及列表和树,则函数算法可能是最明智的.或者,例如,如果您的数据是高度结构化的,如果这是您语言的本机范式,则将其组合为对象可能更有意义 - 或者,它可以像 monad 的功能抽象一样轻松编写,其中是 Haskell 或 ML 等语言的原生范式.

To be clear, the "advantage" of each paradigm is simply in the modeling of your algorithms and data structures. If, for example, your algorithm involves lists and trees, a functional algorithm may be the most sensible. Or, if, for example, your data is highly structured, it may make more sense to compose it as objects if that is the native paradigm of your language - or, it could just as easily be written as a functional abstraction of monads, which is the native paradigm of languages like Haskell or ML.

您选择使用哪种方式对您的项目和您的语言支持的抽象更有意义.

The choice of which you use is simply what makes more sense for your project and the abstractions your language supports.

这篇关于OOP vs 函数式编程 vs 过程式编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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