Compare the Difference Between Similar Terms

Difference Between Declarative and Imperative Programming

Key Difference – Declarative vs Imperative Programming
 

Declarative and imperative programming are two common programming paradigms. The key difference between Declarative and Imperative programming is that Declarative programming focuses on what the program should accomplish while Imperative programming focuses on how the program should achieve the result.

A programming paradigm is used to classify a programming language depending on the feature. It also allows following a certain pattern or style to solve a particular problem.

CONTENTS

1. Overview and Key Difference
2. What is Declarative Programming
3. What is Imperative Programming
4. Side by Side Comparison – Declarative vs Imperative Programming in Tabular Form
5. Summary

What is Declarative Programming?

Declarative programming can be explained using a real-world scenario. Assume that the user needs to check for new emails. One method is by enabling the inbox notifications. The user has to enable the notifications only once, and each time a new email arrives, he gets a notification automatically.  Declarative programming is similar to that. It provides simplicity. Declarative programming expresses what required result is. It explains the logic of a computation without describing the control flow.

Figure 01: Programming Paradigms

An example of declarative programming is as follows. It is to multiply numbers of an array by a constant and to store them into a new array.

var numbers =[1,2,3];

var newnumbers= numbers.map(function(number){

return numbers*5;

});

Console.log(newnumbers);

In the above example, ‘map’ give instructions to iterate each item in the array and to invoke the call back function for each item and to store the return value to the new array. This will give the output 5,10,15. In this program, the main objective of  multiply the numbers by 5 is accomplished using the map function. It will go through each element and use the call back function to calculate and store the values to the new array. It is not required to provide all the steps. The main focus is given to what should be achieved.

What is Imperative Programming?

Imperative programming can be explained using a real-world scenario as before. To check the new emails, the user can login to gmail and keep refreshing the page to check whether he got new emails or not. This is similar to imperative programming. It explains each and every step involved to achieve the result. It uses statements to express the changes in the program state.

Multiplying the arrays elements with a constant and storing the values to a new array in imperative programming is as follows.

var numbers = [1,2,3];

var  newnumbers= [];

for(int i=0; i< numbers.length ; i++) {

newnumbers.push(numbers[i]*5);

}

Console.log(newnumbers);

In the above example, numbers is an array. When going through the loop, each number is multiplied by 5 and added to the newnumbers array. After the end of the loop, the content of the newnumbers will print which are 5,10,15.

It can be observed that the imperative style provides all the steps to achieve the task. It expresses how to iterate through the array using ‘i’ counter variable, how many times to iterate before getting out of the loop and how to insert the calculated values to the new arrays etc.

The same problem was solved using declarative and imperative programming.

What is the Difference Between Declarative and Imperative Programming?

Declarative vs Imperative Programming

Declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow. Imperative programming is a programming paradigm that uses statements that changes the program’s state.
 Main Focus
Declarative programming focuses on what the program should accomplish. Imperative programming focuses on how the program should achieve the result.
Flexibility
Declarative programming provides less flexibility. Imperative programming provides more flexibility.
 Complexity
Declarative programming simplifies the program. Imperative programming can increase the complexity of the program.
Categorization
Functional, Logic, Query programming falls into declarative programming. Procedural and Object Oriented programming falls into imperative programming.

Summary –  Declarative vs Imperative Programming

This article discussed the difference between two major programming paradigms, which are declarative and imperative programming. The difference between declarative and Imperative programming is that Declarative Programming focuses on what the program should accomplish while Imperative Programming focuses on how the program should achieve the result.

Reference:

1.“Declarative Programming.” Wikipedia, Wikimedia Foundation, 3 Apr. 2018. Available here  
2.“Imperative Programming.” Wikipedia, Wikimedia Foundation, 3 Apr. 2018. Available here  
3.Lecture 17 – Imperative vs Declarative Programming, Sam Nxstack, 11 Apr. 2017. Available here