Programming Paradigms: Imperative vs Declarative

In the world of programming, there is not one way for writing programs, instead there are ‘ways’ in which you can write a program or simply put, instruct a computer to do a particular task. These ‘ways’ of programming are known as ‘paradigms’.

Photo by Markus Spiske from Pexels

I’ll give you a silly analogy for explaining what paradigms are, but I think it will work! Let’s say we have two companies, A and B. The end goal for both the companies is to manufacture a Central Processing Unit(CPU). Company A uses RISC (Reduced Instruction Set Computer) architecture while on the other hand company B uses CISC (Complex Instruction Set Computer) architecture for manufacturing the CPU. Again, the end goal is the same, but the way of approaching the final product is different.

Which way you choose, depends upon various factors.

Major Programming Paradigms

There are two major programming paradigms, namely, Imperative and Declarative.

1. Imperative Programming

Imperative programming is a way to program a machine by explicitly stating the control flow (commands/instructions).

For example,

let a = 5;  //sets the value of 'a' to 5

let b = 10;  //sets the value of 'b' to 10

let sum = a+b;  //sets the value of 'sum' to 'a+b'  

console.log( sum );  //finally prints the sum

In the above example, we are explicitly stating the whole process of addition by changing the global state of some variables. This type of programming is known as imperative programming. You have to define exactly what and how you want your computer to perform a certain task.

Now, there are some caveats or disadvantages of imperative programming. One of them is while going for an imperative approach, if you have defined variables in the global scope, redefining them or altering their value unintentionally, can create some issues. Your code becomes hard to manage because it introduces mutability.

2. Declarative Programming

Declarative programming is a way to program a machine by just declaring what you want to do but not exactly how you want it to be done.

For example,

function add (a, b) {
    return a + b;
}

console.log(add(5, 10)) ;

We created a function ‘add’ and then inside that function, added the two numbers and returned the output. Well, then what’s the difference? It’s just adding two numbers, right? There’s more to it. First of all, we have not defined a single variable for the summation of two numbers. We just have a function which accepts two parameters ‘a’ and ‘b’, performs addition operation and returns the output. These type of functions are known as ‘pure’ functions.

Pure functions don’t introduce mutability. There’s no way we can change the output of a pure function for a given input because it doesn’t depend on any state. It will always produce the same output for a given input, thus eliminating any potential side effects.

Some other programming paradigms are:

  • Structured
  • Procedural
  • Object Oriented
  • Functional

That’s all for now. Signing off.

If you have any suggestions, then you can send us a message by going to Contact tab or leave us a comment below..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s