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.
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:
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
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:
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:
Aside from great Benefits, Functional Programming Languages also have very Few or Minimal or Ignorable Drawbacks. They have only the following Cons:
The following concepts are major and important concepts of Functional 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
Any Imperative Programming (IP) Languages can contain the following Characteristics:
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:
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.
Even though OOP solves many real-time problems, still have the following drawbacks(When Compared to FP):
We should go for Functional Programming (FP) in the following scenarios:
We should go for Object Oriented Programming (OOP) in the following scenarios:
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).
|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.
Click here to Sign up and get $200 of credit to try our products over 60 days!