Functional Programming vs. Object Oriented Programming

Elizabeth Price Developer Tips, Tricks & Resources

In current development, Object Oriented Programming has proven more popular than its predecessor Functional Programming.  Functional Programming was developed before Object Oriented Programming, with support for Lisp, Clojure, Wolfram, Erlang, Haskel, F#, and R.  Object Oriented Programming is more modern with support for C++, C#, Java, Python, Ruby. PHP, Perl, Objective-C, Swift, and Dart.  With the increasing popularity of JavaScript, Functional Programming is experiencing a resurgence in popularity.

Definition

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.

Method

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.

Data

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. 

Paradigm

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

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

}

print a 

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. 

Use

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. 

Resurgence of Functional Programming with Popularity of JavaScript

Functional Programming has made it’s big comeback with the popularity of JavaScript and its component-based pattern libraries, such as Angular, Vue and React.  React, for example, has a functional-based design and the objects in it are immutable. 

JavaScript has first class functions, and can be passed as any other values. That means that a function can be passed to another function, which makes higher-order functions. They are mostly used to abstract actions, async flow control, and create utilities.

Conclusion

With differences in their definitions, methods, and uses, Object Oriented Programming is more popular in current development than its precursor Functional Programming.  But, Functional Programming is experiencing a resurgence with the increasing popularity of JavaScript and languages such as Swift and Scala combining the two programming methodologies.

About Elizabeth Price

Elizabeth Price is a freelance writer with expertise in education, marketing, and tech-related topics. Deeply interested in computer sciences, she studies frontend frameworks online and strives to improve the navigational components of EssayPro, a website where she used to work as a blog editor. You can reach out to her on Twitter or drop her a letter to elizabethprice993@gmail.com.