why to use functional programming in java? object oriented vs functional programming in java ?

aditya chaudhari
JavaDeveloperDiary — JDD
5 min readSep 30, 2021

In this post we are going to talk about following questions :

Q. Why we need functional programming in java ?
Q. How we can embrace new way of functional programming thinking while coding in java ?
Q. Why FP is change in thinking as compared to OOP ?
Q. How java achieved provision of functional style in java language ?

Programming languages are generally classified in three types :
1) procedural ( aka imperative )
2) Object oriented
3) Functional (aka declarative )

Java has been supporting imperative style programming and object oriented style programming since long time.
imperative style programming is where we specify well-structured steps and procedures to compose a program. Here we tell every step what we want it to do .
Object-oriented programming (OOP) is about encapsulating data and behavior into objects. in OOP objects interacts with each other use data and behavior(methods) and does the job for us.

However in functional programming data itself is code to other piece of code. Everything in functional programming revolves around pure functions where to achieve a task function is being called which internally may call another function.

The principles of functional programming are centered around the idea of pure functions .

Some characteristics of pure functions are :

  • functions return the same output for a given set of inputs
    This means functions are not allowed to use any global variable or context. Every time in every case for same inputs same output will be returned.
  • referential transparency ( aka higher-order function )
    This refers to the ability to pass code(function) as an argument(data) to another function.
  • no side effects
    pure functions are not allowed to modify any argument or any global value they are dealing with . It means there is no impact of external parameter on the output of function.

Q. Why we need functional programming in java ?

Java programming is more equipped with doing imperative and OO style programming where we use variables, objects and behaviors(methods) and states(variables).
However there are ways we can program without mutating variables and object states. OOP and imperative style help in terms of maintainability and creating maintainable software’s model but it has been observed its more verbose and error prone because of shared things.

In OOP and imperative style everything revolves around using Classes, variables and objects to manipulate data.
However in functional programming its more about using objects, functions .

Below are some of the functional programming characteristics which provide more tools to write more robust and clear code.
1) Functional programming makes composition and recombination much easier as they are more modular because they’re built from functions that have only input and output.
2) Functional programs are easier to understand . One specific input will always give the same output.
3) Functional programs are inherently thread-safe because they avoid mutation of shared state.
4) Functional programs are more robust than their imperative counterparts.

Object oriented programming makes code understandable by encapsulating moving parts. Functional programming makes code understandable by minimizing moving parts. — Michael Feathers

Why to use functional programming in Java?

  1. Extend and implement different policies, contracts in better ways.
    Interfaces is our tool to extend or implement policies however there is better way we can provide different policies in less code which is using lambda expressions and functional programming .
  2. Better handling of concurrent code using parallel streams.
  3. Telling a problem statement and solution in more expressive way instead creating own model to state problem and its solution .
    OOP is more like creating own version of problem statement and provide solution to it using OOP principles. Problem here is many times we need to learn “other developers” style to understand problem and solution .
    In Functional programming its like using simple language to write code artifacts which looks expressive like everyday language we use.
  4. Separation of concern
    Separation of concern is about encapsulating similar things together and program to minimize coupling and increased cohesiveness.
    In OOP this is been achieved by multiple design patterns which internally use classed , interfaces and objects.
    In Functional programming there are better ways to implement design patterns or attain more separation of concern.

Q. How we can embrace new way of functional programming thinking while coding in java ?

  1. Be Declarative : In traditional java coding practice to achieve a task we create multiple variables and objects and play with their states this is non-declarative way.
    Functional programming expects us to express code in more declarative way and write low level details in another functions without mutate states of arguments we pass to other functions.
  2. Promote Immutability : Mutable code has many moving parts. The more things change, the easier it is for components to break and for errors to creep in. Java supports immutability but does not enforce it but we can using functional interfaces .
  3. Prefer expressions over statements : Statements are stubborn and force mutation. Expressions promote immutability and function composition.
  4. Design with higher order functions :
    In OOP we’re used to passing objects to methods, creating objects within methods, and returning objects from within methods. Higher-order functions do to functions what methods did to objects. With higher-order functions we can Pass functions to functions, Create functions within functions, Return functions from functions

Q. Why FP is change in thinking as compared to OOP ?
In traditional OOP and imperative style programming input data is being manipulated by writing code using constructs like classes, variables and objects . However in functional programming data is manipulated using functions which internally may call some other functions . Each function know its responsibility and does not mutate with different inputs.

Q. How java achieved provision of functional style in java language ?
Using lambda expressions and functional interfaces. (read further …)

Thanks for reading.
If you have any suggestions regarding topic or post please let me know in comments . If you like content please encourage me to write more by applause on this post.

All java 8 related articles on this publication can be found here.

--

--

aditya chaudhari
JavaDeveloperDiary — JDD

building efficient, scalable and maintainable enterprise e-commerce applications using java, spring framework and SAP CC. Life Mantra → Love IT Live IT Enjoy IT