Functional Programming treats programs as functions and is assigned using a mix of recursions. Object Oriented Programming is a convolution of cooperating objects. Every object inside the program can receive a message, process data, or send messages to other objects. In Functional Programming, the function is the primary unit that can be changed. In Object Oriented Programming, the object is the primary unit that can be changed.
With Object Oriented Programming, everything is imperatively described with the code describing how the program works. The commands go through the machine, statements are executed, and then appear in the right order. If the order is not right, the program will not run.
Functional Programming utilizes the more flexible declarative programming. In Object Oriented Programming, the command words would be: “You must do this” and the program would run smoothly. In functional programming, you could tell the program: “You should do this” without further specifications, and the program would run without any issues.
Functional Programming is a more “flexible” approach, with mutable objects. These objects can be changed, even after they have been created. In contrast, Object Oriented Programming uses immutable objects that cannot be altered.
While Functional Programming and Object Oriented Programming use mutable and immutable objects, they do have a type. Mutable fields can be changed after object creation and have a method for modifying the field value. Immutable objects cannot be changed after object creation and method for modifying the field value does not exist.
Drawing a parallel
Parallel Programming is defined as the calculations or the execution of processes carried out simultaneously. Functional Programming supports Parallel Programming, while Object Oriented Programming does not.
Parallel Programming is a separate branch of programming where special methods are used to separate algorithms into their basic parts. The methods are performed on several machines or CPUs at the same time. Hence the term, parallel.
By using Parallel Programming, computing time is significantly reduced with quicker results compared to that of Non-Parallel Programming. Although, at times, Parallel Programming is not always the most time efficient programming method and a different method is needed.
The programming paradigm is the execution or “philosophy” of certain programming languages. With this, the programming languages have been separated into two basic groups: imperative languages and declarative languages.
The basic divisions are further broken down into Structural or Classic “Imperative” Programming, Logical Programming, Functional Programming, and Object-Oriented Programming.
Structural Programming is often thought of as the introduction into programming as it commands your machine through branching and cycles. Logical Programming is most often used in artificial intelligence development with problems explained in detail. Functional programming is defined as a function. Object-Oriented Programming interprets real-world issues and situations and is the most popular programming method in current development.
Iteration serves as an additional difference between distinctive between Functional Programming and Object Oriented Programming. Iteration can best be defined as something that is repeated several times, with the purpose of getting close to the same result or goal.
Every kind of repetition in programming is called iteration. More specifically, the repetition of command blocks. When one iteration ends, the results from it are used as a starting point for the next iteration, and so on.
Below is an example of an iteration:
a = 0
for i from 1 to 3 // goes through the loop 3 times
a = a + i // current value sized up for i
Functional programming uses recursion for iterative data, whereas Object Oriented Programming uses loops. Recursion is an infinite, scaling repetition of the same pattern – ascending or descending. The method in the program will be called each time the program is executed.
Iterations (loops) are the instructions in a program that are executed over and over again. Iterations are always applied to a set of certain instructions while recursions are always related to a method.
If a recursion does not have a brake, or condition, it will result in blocking the computer by using all of its resources. In contrast, if a variable is not terminated, the iteration will last indefinitely. Functional Programming tends to be more compact than Object Oriented Programming with recursions making code a bit shorter and iterations making it longer.
There are no definitive rulings for when to use Functional Programming and when to use Object Oriented Programming. At times, Object Oriented Programming is not the cleanest way to program. In order to reach a perfect solution, software developers can over engineer their code. Functional Programming can prove difficult for junior developers to understand and execute.
Object Oriented Programming and Functional Programming are not mutually exclusive. Popular programming languages, such as Swift and Scala, mix the two programming methodologies.
- Functional Programming vs. Object Oriented Programming - November 8, 2019