Tutorial

Compare Functional Programming, Imperative Programming and Object Oriented Programming

Published on August 3, 2022
author

Rambabu Posa

Compare Functional Programming, Imperative Programming and Object Oriented Programming

As Oracle Corporation has introduced some Functional constructs in Java SE 8, now-a-days most of the Interviewers are interested to ask some questions about Functional Programming. As a Java/Groovy/Scala Developer or any Functional Programming Developer, we should learn the following questions and answers to clear interviews:

  1. What is Functional Programming?
  2. What are the “Pros & Cons” of Functional Programming?
  3. What are the differences between Functional Programming and Imperative Programming?
  4. What are the differences between Functional Programming and Object Oriented Programming?
  5. What are the differences between Functional Programming, Imperative Programming(IP) and Object Oriented Programming?
  6. What are the major advantages of Functional Programming over Imperative Programming or Object Oriented Programming?
  7. When to use Functional Programming?
  8. When to use Object Oriented Programming?
  9. What are the drawbacks of OOP?
  10. What are the drawbacks of OOP-Inheritance? … And More

In this post, we are going to discuss about the following three popular Programming paradigms one by one and also answer above questions:

  1. Functional Programming (FP)
  2. Imperative Programming (IP)
  3. Object Oriented Programming (OOP)

What is Functional Programming?

In simple words, Functional Programming (FP) is one of the popular Programming paradigms, which does computation like Mathematical Functions without Changing State and Mutating Data. In Functional Programming, Functions are first class candidates. We write programs by defining a set of Functions and Immutable Data. Examples of FP Languages:- Scala, Haskell, Earlang etc are popular FP Languages. Java SE 8 also have some Functional Constructs (Please refer Java 8 posts for more details)

What are the Characteristics of FP?

Functional Programming Languages like Scala have the following Characteristics

  1. State does not exist. FP Programs does not contain state. That means all Data is Immutable Data and Functions cannot change state.2. Low importance of Order of Execution In FP Languages, we write programs with a set of Independent Functions. Functions contain a set of statements. In FP, the order of execution of those Functions does not have much importance because they do not have state and all Functions work independently. Even if we change the order of execution still they produce the same results.3. Stateless Programming Model All FP Programs uses Immutable data and Functions, which cannot modify that data. That means FP Languages support Stateless Programming Model.4. Functions are first class citizens In FP Languages, Functions are first class objects. Functions are independent units, we can execute them in any order.5. Primary Manipulations Units In FP Languages, Primary Manipulations units are Functions and Data Structures because All Programs are made up of using these units.6. Modular Programming In FP Languages, we need to write smaller and independent units, called Pure Functions to support Stateless Programming model. That means FP supports better Modularity than OOP.7. Higher-order Functions and Lazy Evaluation Functional Programming Languages should support Higher-order functions and Lazy Evaluation features.8. Primary Flow Controls FP Languages don’t use Flow Controls like For…Loop, Do…While Loop, While…Loop etc and also don’t use Conditional statements like If…Else or Switch Statements. All FP Languages write programs using the following things:
  • Functions
  • Function calls
  • Function calls with Recursion
  1. Abstraction,Encaspulation,Inheritance and Polymorphism Like OOP, FP Languages supports all 4 concepts : Abstraction,Encaspulation,Inheritance and Polymorphism. FP Languages supports Inheritance with Type Classes or Implicits. They support Polymorphism with the help of Generics. It is also known as Parametric Polymorphism.

What is the main Focus of FP?

Unlike OOP Languages, All FP Language Programs mainly focus on “What you are doing” or “What is to be done”. They mainly focus on the following things:

  • What Information is desired that is Inputs.
  • What Transformations are required that is Actual Logic.

That means FP is mainly focused on “What is to be done”. It does not much focus on “How is it to be done”. That’s we can write Functional Programming just like Problem Domain description. That’s why, not only Developers but also other people can understand FP code very easily. Now we will discuss about the “Pros & Cons” (Advantages and Disadvantages) of Functional Programming.

Advantages of Functional Programming?

Functional Programming Languages have the following Advantages or Benefits or Pros:

  • Bugs-Free Code As FP Languages do not support state, they don’t raise any side-effects that means we can write Error-free code or Bugs-free code or Less Error-prone Code.- Efficient Parallel Programming As FP Languages have NO Mutable state, they don’t raise any state-change issues. That means they use only Immutable Data. They use Independent Units to write programs that is “Functions”. We can write very efficient Parallel or Concurrent Programming because they run independently without changing state.- Better Performance As FP Programs compose with all Independent units, They can run Parallel or Concurrently. Because of this reason, FP Applications gain better performance.- Better Encaspulation Unlike OOP, FP supports better Encaspulation with Pure Functions. Pure functions means without side-effects.- Supports Nested Functions Nested Functions means composing functions with-in other functions to solve problems. FP supports Nested Functions.- Increase Reusability As FP programs are made up of Independent Units that is “Functions”, we can reuse them very easily.- Better Modularity In FP Languages, we need to write smaller and independent units, called Pure Functions to support Stateless Programming model. That means FP supports better Modularity than OOP.- Easy Lazy Evaluation In FP Languages, it is very easy to write Lazy Evaluation. They support Lazy Functional Constructs like Lazy Lists, Lazy Maps etc.- Increase Readability and Maintainability Functional Programming (FP) also improves Readability and Maintainability because they work Independently and they don’t change state.- Increase Testability As we write our FP programs using Independent Units that is “Functions”, we can unit test them very easily.- Supports Abstraction over Behavior Unlike OOP, FP supports both “Abstraction over Data” and “Abstraction over Behavior”. Because Real-world contains both.- Support for BigData As FP supports Parallel programming and better performance, FP is very good for developing BigData Applications.- Robust and Reliable Code As FP uses Immutable Data, we can easily develop Robust and Reliable Code using FP.

Drawbacks of Functional Programming?

Aside from great Benefits, Functional Programming Languages also have very Few or Minimal or Ignorable Drawbacks. They have only the following Cons:

  • Requires Lot of Memory FP don’t have state. They always creates new Objects to perform actions instead of modifying existing objects. Because of this, FP Applications takes lot of memory.- Does NOT concentrate on Liskov Substitution

What are the major concepts of Functional Programming?

The following concepts are major and important concepts of Functional Programming.

  • First-Class Functions.
  • Lazy Evaluation.
  • Higher-Order Functions.
  • Immutability(Non-Mutable Data).
  • Modularity.
  • No Side-effects.
  • Lazy Evaluation.
  • Recursive Function-Calls.

What is Imperative Programming?

Imperative Programming (IP) is one of the popular Programming Paradigms which executes a sequence of steps/instructions/statements in some order. Examples of IP Languages:- Java, C, C++ etc

Main Characteristics of Imperative Programming?

Any Imperative Programming (IP) Languages can contain the following Characteristics:

  • Sequence of Statements.
  • Order of execution of Statements is very important.
  • They contain state.
  • They use both Immutable and Mutable Data.
  • They can change state.
  • They may have Side-effects.
  • Stateful Programming Model.
  • They directly change the state of Program.
  • They represent state with Data Fields.

What is Object Oriented Programming?

Object Oriented Programming is another kind of Programming Paradigm. It represents everything as an Object. Each Object contains Some Data Fields and Methods. All OOP Programs contains State. They use Mutable Data and Data Structures. Like FP, We can write complete programmings by using Immutable Data, but it does not enforce this rule. Object Oriented Programming (OOP) is a super set of Imperative Programming. It follows all characteristics of IP with some extra features. Those extra features are:

  • Everything is an Object.
  • Each Object contains Some Data Fields and Methods.
  • OOPs Concepts: Abstraction,Encaspulation,Inheritance and Polymorphism

Unlike Functional Programming Languages, OOP Languages are mainly focused on “How is it to be done”. That means As a Developer, we focus on “How you are doing”. Moreover, OOP combines both “What you are doing” and “How you are doing”. That’s why we cannot write concise and more readable code. Only Developers can understand the code, Other people can get more confusion to understand the application code, they cannot understand it.

Drawbacks of Object Oriented Programming(OOP)?

Even though OOP solves many real-time problems, still have the following drawbacks(When Compared to FP):

  • It does not support full Reusability.
  • It is not fully Modularity.
  • It breaks Encaspulation concept.
  • Inheritance has lot of drawbacks.

Major Drawbacks of Inheritance:

  • Breaks Encapsulation principle
  • When Inheritance levels increases, it is very tough and hard to maintain and create objects.

When to use Functional Programming?

We should go for Functional Programming (FP) in the following scenarios:

  • When we are going to be performing lots of different operations on the data that has fixed.
  • In other words, when we have few things with more operations.

When to use Object Oriented Programming?

We should go for Object Oriented Programming (OOP) in the following scenarios:

  • When we are going to be performing few operations on lots of different variants which have common behavior.
  • In other words, when we have more things with few operations.

NOTE:- Here Things are Real-world objects and operations are Real-world Actions. For example, In Java we represent these Real-world Things as “Classes” and Real-world Actions as Methods(Operations).

Differences between FP and OOP(IP)?

Functional Programming OOP
Does not exist State Exists State
Uses Immutable data Uses Mutable data
It follows Declarative Programming Model It follows Imperative Programming Model
Stateless Programming Model Stateful Programming Model
Main Fcous on: “What you are doing” Main focus on “How you are doing”
Good for Parallel (Concurrency) Programming Poor for Parallel (Concurrency) Programming
Good for BigData processing and analysis NOT Good for BigData processing and analysis
Supports pure Encaspulation It breaks Encaspulation concept
Functions with No-Side Effects Methods with Side Effects
Functions are first-class citizens Objects are first-class citizens
Primary Manipulation Unit is “Function” Primary Manipulation Unit is Objects(Instances of Classes)
Flow Controls: Function calls, Function Calls with Recursion Flow Controls: Loops, Conditional Statements
It uses “Recursion” concept to iterate Collection Data. It uses “Loop” concept to iterate Collection Data. For example:-For-each loop in Java
Order of execution is less importance. Order of execution is must and very important.
Supports both “Abstraction over Data” and “Abstraction over Behavior”. Supports only “Abstraction over Data”.
We use FP when we have few Things with more operations. We use OOP when we have few Operations with more Things. For example: Things are classes and Operations are Methods in Java.

That’s it all about three popular Programming Paradigms. NOTE:- I’m basically from OOP, but started working on FP just a year ago. So if Functional Programming Experts finds any mistakes in this post, please provide me your valuable inputs. Please drop me a comment if you like my post or have any doubts or suggestions.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Rambabu Posa

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
September 10, 2019

FP is more abstract, for some people hard to comprehend … therefore it more complex, more error prone, harder to test (try to write a test of some real world complex high order function or curried function). FP beraks encapsulation since any function can change global shared data (state). FP has much more drawbacks. FP is not better than OOP, it is just more eligible for certain tasks therefore thre sould be equal cons and pros because one con of FP is pro in OOP and vice versa. Clever developer use both paradigms. More precisely most developers use imperative style with functions and classes but calls himself FP developer because it is mainstream now :)

- Jiri

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 10, 2018

    Looks like most of the content is based on personal opinion not based on facts.

    - A Li

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      February 13, 2018

      Very nice tutorial sir. Thank you.

      - Ashok

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        December 1, 2017

        WoW Loved it . . Tahnk You . .

        - Tapas Das

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          November 18, 2017

          Nice explanation

          - Dinesh and devender

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            August 5, 2017

            In FP, “State does not exist”? exist != change

            - John

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              May 15, 2017

              Very clear explanation

              - charles

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                January 17, 2017

                Nice one.

                - Srao

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  January 9, 2017

                  waow, very nice tutorial, thanks.

                  - batuhan çağlayan

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    December 7, 2016

                    Very helpful. Thank you.

                    - Kkona

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Featured on Community

                      Get our biweekly newsletter

                      Sign up for Infrastructure as a Newsletter.

                      Hollie's Hub for Good

                      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

                      Become a contributor

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      Welcome to the developer cloud

                      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Learn more