编程范式是一种编程风格或“方式”。编程范式因它们支持的特性和风格而异。有几个特性决定了一个编程范式,例如模块化、对象、中断或事件、控制流等。每个编程范式都有自己的优势,所以在实际使用之前最好知道在哪里使用它。
根据堆栈溢出中给出的答案,
当您对事物有一组固定的操作时,面向对象的语言是很好的,并且随着您的代码的发展,您主要添加新事物。这可以通过添加实现现有方法的新类来实现,而现有的类则保持不变。
当你有一组固定的东西时,函数式语言是很好的,随着你的代码的发展,你主要在现有的东西上添加新的操作。这可以通过添加使用现有数据类型计算的新函数来实现,而现有函数则保持不变。
也可以根据我们自己的需要同时使用这两种编程范式。因为我们有Python, Java等语言,既支持面向对象的概念,又通过支持各种内置函数来实现功能。
函数式编程与面向对象编程
Functional Programming | Object Oriented Programming |
---|---|
This programming paradigm emphasizes on the use of functions where each function performs a specific task. | This programming paradigm is based on object oriented concept. Classes are used where instance of objects are created |
Fundamental elements used are variables and functions.The data in the functions are immutable(cannot be changed after creation). | Fundamental elements used are objects and methods and the data used here are mutable data. |
Importance is not given to data but to functions. | Importance is given to data rather than procedures. |
It follows declarative programming model. | It follows imperative programming model. |
It uses recursion for iteration. | It uses loops for iteration. |
It is parallel programming supported. | It does not support parallel programming. |
The statements in this programming paradigm does not need to follow a particular order while execution. | The statements in this programming paradigm need to follow a order i.e., bottom up approach while execution. |
Does not have any access specifier. | Has three access specifiers namely, Public, Private and Protected. |
To add new data and functions is not so easy. | Provides and easy way to add new data and functions. |
No data hiding is possible. Hence, Security is not possible. | Provides data hiding. Hense, secured programs are possible. |