Declarative vs Imperative Programming

When we talk about the programming we mainly focus on languages and codes but an important part of writing good code or selecting the correct programming language for the problem we are working on is to understand the language itself. In this article, we will discuss the different types of Programming Paradigm which are present in programming.

Let us first understand the meaning of Programming Paradigm, it may sound something difficult but it simply is a division of languages based on how they work. There are mainly 4 types of paradigm: 

  • Imperative or Procedural Programming
    • E.g. C, Pascal etc.
  • Declarative Programming
    • E.g. SQL
  • Logical Programming 
    • E.g. Prolog
  • Object Oriented Programming
    • E.g. Java, C++ etc.

Let’s see an example to understand the difference between Imperative and Declarative Programming. Suppose you are trying to build a house, if you go to your engineer and tell him how many rooms you want and where you want them then you will come under a declarative programming paradigm. On the other hand, if you tell your engineer that you should follow these particular steps (e.g. build a wall here etc.) and then my house will be built then you will come under imperative programming.

Declarative paradigm expresses what to do.

Imperative paradigm expresses how to do it.

In an imperative language, we declare each and every step which needs to be done, like in C where we go with the logical flow of the program and write that down in steps.

In declarative language, we just specify what we want and the languages under the hood do the work for us and bring us the result. In SQL we just write which entries we need from a table and then it directly gives us the result, we do not specify how it should search in a table or anything else.

Imperative languages, in general, are easy to understand as the flow is clear to programmers but have long code. Flow in declarative languages isn’t that clear and therefore is difficult to debug but as we just mention steps the code length is short.

Declarative vs Imperative Programming

Declarative Imperative
The developer generally write code about what needs to be done Developer write code about how something should be done
Code length is short in general Code length is generally large
Logic Programming and functional programming falls under the Declarative Paradigm  Structured Programming and Procedural programming falls under Imperative Paradigm
Difficult to debug Easy to debug 
E.g. SQL, Prolog E.g. C, C++

Note: All the programming paradigms are not necessarily disjoint, the same language may follow traits from two different paradigms.

I hope now you got a clear idea about both programming paradigms. Comment down below if you have any queries.

Leave a Comment
