What is the difference between Imperative Programming & Declarative Programming?

Anna Vu
Anna Vu
Published in
3 min readMay 22, 2020

If you’ve used JavaScript, you would at least come across the terms “Imperative Programming” or “Declarative programming” once. So what are they? and what is the relation between them and JavaScript? Is it important to understand their concept?

My honest answer is yes. By comprehending imperative programming and declarative programming, it helps developers make good decision on how to approach the problem. Before delving into imperative and declarative programming, let’s have an overview of paradigm.

What is Paradigm?

Imperative and declarative programming are 2 out of 27 programming paradigms.

A programming paradigm is an approach to programming a computer based on a mathematical theory or a coherent set of principles.

Each paradigm supports a set of concepts that makes it the best for a certain kind of problem (Roy, P).

Programming paradigms influence programming language designs. For instance, object-oriented programming languages such as Java, C++, C#, Oz, PHP, etc. are best for problems with large number of data abstraction organized in a hierarchy, while logic programming languages including Prolog, Datalog, etc. are suitable for problems that involve transformation or navigation of complex symbolic structures.

You can learn more about paradigms and its supportive languages in the figure below.

Figure 1: Taxonomy that shows how paradigms are related. (Source)

In figure 1, there are 27 boxes in which each box represents a paradigm. The languages mentioned under the paradigm are that paradigm’s supportive languages. Programming languages can be definitive meaning they might support 4 different paradigms in a layer structure or dual-paradigm which they support 2 paradigms (Roy, P)

Imperative Programming

Imperative programming (from Latin imperare = command) is a paradigm that uses statement to change a program’s state. A program based on Imperative paradigm is made up of clearly-defined sequence of instructions.

In another word, with an Imperative approach, developers tell the program HOW to solve the problems by writing every single line of instruction just like Bob Ross shows you how to paint an island in the wilderness step by step.

Popular languages that primarily support Imperative programming include Java, C++, C#, etc.

Declarative Programming

Unlike Imperative programming, Declarative programming describes the properties of the result but not actually telling the program how to compute the result.

In practice, this approach entails providing a domain-specific language (DSL) for expressing what the user wants, and shielding them from the low-level constructs (loops, conditionals, assignments) that materialize the desired end state (Pereiro, F)

In simpler word, Declarative programming instructs a program on WHAT needs to be done instead of telling it how to do it. Unlike Bob Ross, your art professor doesn’t care how you’re going paint as long as you have an island in the wilderness painting submitted to him.

You will get familiar with this paradigm when you adopt React.js or Vue.js, etc.

Why choose Imperative or Declarative?

With Imperative Programming, it makes the programs simpler for human to read and write. Plus, all of the machine code that the computer execute is written in imperative style.

With Declarative Programming, since developers don’t have to show the program how to do it, Declarative programming becomes more succinct, easier to read and to be maintained. It also enables the reiteration development with easier testing and debugging.

Advantages of Declarative Programming
Picture 1: All advantages of Declarative Programming (Source)

--

--