Functional Programming and Object Oriented Programming
I’ve been looking into functional programming for about a week now, and I must say it is quite an interesting programming paradigm.
In functional programming, computation is treated as the evaluation of mathematical equations rather than changing the state of objects or mutable data.(See wikipedia definition here). This style of writing has numerous advantages such as making testing our software easier and writing more clear and concise code. Object oriented programming on the other hand is a paradigm in which data and the operations that can be performed on the data are bundled together in a class. The two paradigms have differences which I will not get into but follow this link to see for yourself. Now, on to the purpose of this article
How to choose which paradigm to use
Both functional and object oriented paradigms can be used to solve the same problems, the only difference would be that solving the same problem would require taking different approaches. Choosing what paradigm to use would largely be influenced by what advantages accrue from the paradigm and what trade-offs you are willing to make.
To use a functional programming principles, you would have to write pure functions, that is, functions that produce no side effects(does not change state of local static variables or non local variables) and whose return value is the same for the same arguments. One can also use the functional programming paradigm by writing higher order functions. A higher order function is one whose arguments are functions or whose return value is a function.
A functional programming language (or the principles of the paradigm) would be best to use when we want to write clear concise code that is easy to test. This is because code written following functional principles is often much shorter than that written using object oriented principles, also since there are no side effects produced in functional programming, we developers know what to expect from functional calls. This makes it easier to catch bugs in our code and make necessary changes. Also because of the immutability, parallel programming using functional languages is easier. Functional programming is suitable in situations such as language processing where we perform many different operations on the same set of data and when doing complex mathematical calculations.
Object Oriented Programming
Objects in OOP are basically a representation of a process or a real world object that focus on the most important attributes of the process or object. Classes in object oriented programming are blueprints from which objects are created. Object oriented languages offer numerous constructs with which to meet the principles of object oriented programming. Object oriented programming focuses on encapsulating data. To achieve inheritance child classes extend their parents and inherit both data and methods. Interfaces allow programmers to make use of polymorphism by defining behaviour that is common to many objects. Using these and other constructs we are able to use object orientation.
Object oriented languages like Java and C++ are suitable for creating interactive applications and graphical user interfaces. This paradigm allows us to write reusable code which is easily maintainable.
Functional programming is an improvement on object oriented programming in many ways, especially because it allows us to write code that is easy to think about since we don’t have to reason about the change of states due to immutability. I personally believe that it is an important tool for a programmer to have in their toolbox.