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:
What is Functional Programming?
What are the “Pros & Cons” of Functional Programming?
What are the differences between Functional Programming and Imperative Programming?
What are the differences between Functional Programming and Object Oriented Programming?
What are the differences between Functional Programming, Imperative Programming(IP) and Object Oriented Programming?
What are the major advantages of Functional Programming over Imperative Programming or Object Oriented Programming?
When to use Functional Programming?
When to use Object Oriented Programming?
What are the drawbacks of OOP?
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:
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)
Functional Programming Languages like Scala have the following Characteristics
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
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.
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.
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.
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
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
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.
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).
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.
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.
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 :)
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
Looks like most of the content is based on personal opinion not based on facts.
- A Li
Very nice tutorial sir. Thank you.
- Ashok
WoW Loved it . . Tahnk You . .
- Tapas Das
Nice explanation
- Dinesh and devender
In FP, “State does not exist”? exist != change
- John
Very clear explanation
- charles
Nice one.
- Srao
waow, very nice tutorial, thanks.
- batuhan çağlayan
Very helpful. Thank you.
- Kkona