Declarative vs Imperative in JAVA

4 min read

Hey guys!! In this post we will be learning two styles of writing code and their comparisons and effects on coding.

What is Imperative Style?

Imperative is also known as how-to style of programming, wherein we specify step by step procedure of accomplishing any objective. Below are the main focus points of this style.

  1. Focus on how to perform operations.
  2. Embraces/inclined towards object mutability.
  3. We have to write the code on what needs to be done in each step of instruction.
  4. Imperative style is used with classic Object Oriented Programming.

What is Declarative Style?

Declarative is also known as what-to style of programming, wherein we just specify what inbuilt operations we want to use to accomplish the objective. None of our concern how framework actually implements those inbuilt methods. Below are the main focus points of this style of programming.

  1. Focuses on what result we want.
  2. Embraces/inclined Object immutability.
  3. Analogous to SQL.
  4. Uses in built functions to achieve objective.
  5. Declarative style is used with Functional Programming.

Below are few examples which will give more clearer understanding of both the style of prgramming.

public class DeclarativeVsImperativeSample1 {

    public static void main(String[] args) {
        /**
         * Objective - Sum of integers
         * Imperative - how to!
         */
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum+=i;
        }
        System.out.println("Sum of integers in imperative style: " +sum);

        /**
         * Objective - Sum of integers
         * Declarative - what to!
         */
        int sumByStream = IntStream
                .rangeClosed(0,100)
                .sum();
        System.out.println("Sum of integers in declarative style: " +sumByStream);

    }
}

In the above code, we can deduce how much more readable and concise is the declarative approach. In the imperative approach, we had to specify in each step what we want to do, like, declare a sum variable, iterate on integers, sum all integers and than print. Wherein in declarative approach, we just specified what we want to achieve and rest everything is done by inbuilt methods.

public class DeclarativeVsImperativeSample2 {
    public static void main(String[] args) {
        /**
         * Objective - Remove duplicates from list of integers
         * Imperative - how to!
         */

        List<Integer> integerList = Arrays.asList(1,2,3,4,4,5,6,7,7,8,8,9,20);
        List<Integer> uniqueList = new ArrayList<>();
        for (Integer integer: integerList){
            if (!uniqueList.contains(integer)){
                uniqueList.add(integer);
            }
        }
        System.out.println("Unique list by imperative approach: " +uniqueList);
        /**
         * Objective - Remove duplicates from list of integers
         * Declarative - what to!
         */
        List<Integer> listOfUniqueIntegers = integerList.stream()
                .distinct()
                .collect(Collectors.toList());
        System.out.println("Unique list by declarative approach: " +uniqueList);
    }
}

Again, We can infer the gain in readability in the code with respect to declarative approach.

Hope, you guys had an understanding of both the style of programming.

Happy Learning!!