Object Oriented vs. Functional Programming
What is Object Oriented Programming?
- According to Wikipedia, object-oriented programming (OOP) is a programming paradigm that represents the concept of "objects" that have data fields (attributes that describe the object) and associated procedures known as methods. Object-oriented programming is an approach to designing modular, reusable software systems. The real key to the object-oriented approach is that it is a modelling approach first.
- Object-oriented programming can be best described as a programming language model organized around objects rather than "actions" and data rather than logic.
- Java, Python, C++, Visual Basic .NET and Ruby are some examples of the most popular OOP languages.
- In summary, object-oriented programming is a programming paradigm that uses abstraction to create models based on the real world. It uses several techniques from previously established paradigms, including modularity, polymorphism, and encapsulation.
What is Functional Programming?
- According to Wikipedia, functional programming is a programming paradigm, a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids state and mutable data. Functional programming languages, especially purely functional ones such as Hope and Rex, have largely been emphasized in academia rather than in commercial software development due to lack of reusability and modular approach as compared to object oriented programming languages.
- Functional languages use a different paradigm than both imperative and object oriented languages.
- Functional languages are used especially where the code correctness is of special importance. It is much easier to provide a (usable) formal sematics for functional languages than for stateful languages (imperative languages).
Comparison of Object Oriented vs Functional Programming
- One of the principal advantages of object-oriented programming techniques over procedural/functional programming techniques is that they enable programmers to create modules that do not need to be changed when a new type of object is added. A programmer can simply create a new object that inherits many of its features from existing objects. This makes object-oriented programs easier to modify.
- Object-oriented programming is intended to promote greater flexibility and maintainability in programming, and is widely popular in large-scale software engineering. By virtue of its strong emphasis on modularity, object oriented code is intended to be simpler to develop and easier to understand later on as compared to other programming paradigm including functional, lending itself to more direct analysis, coding, and understanding of complex situations and procedures than less modular programming methods.
- Functional programming treats computation as the evaluation of mathematical functions and avoids state and mutable data, whereas object-oriented programming organizes programs as objects: data structures consisting of datafields and methods together with their interactions.
- Functional programming paradigm was explicitly created to support a pure functional approach to problem solving. Functional programming is a form of declarative programming. In contrast, most mainstream languages, including object-oriented programming (OOP) languages such as C#, Visual Basic, C++, and Java, were designed to primarily support imperative (procedural) programming.
- However, there are certain situations and scenarios where functional programming and functional programming languages are a great fit and probably the better approach. For pure mathematical calculation, functional programming is actually clearer than imperative programming. However, for business software and other general application software, exactly the opposite is true and object oriented programming approach is preferred over functional programming.