Imperative vs Declarative

Imperative code: Tell the machine what to do and how to do it.

Declarative code: Tell the machine what to do what should happen. But It does not tell the computer how to do things.

Computers are better at being imperative that is it needs to know how to do things. Humans are more declarative.

If you order pizza, you do not have to tell how to make pizza.

Machine code is very imperative. more higher level languages are more declarative. You do not have to say this is where you should store the memory.

Imperative code

for (let i = 0; i < 10; i++) {

This is pretty imperative: we say declare variable zero and then loop for a ten times and then also increment by 1. Also, console.log(i) value. This is a lot of instruction.

More Declarative code

[1,2,3,4,5,6,7,8,9,10].forEach(i => console.log(i));

This is more declarative. I order console.log(i) a array value with array values times.

Jquery is a lot of more imperative than React, Angular, and Vue.

Jquery we told our website more how to do.

React is fairly declarative. You do not tell how to do display.

Functional programming helps us be more declarative with composing function. Declarative code is always going to end up either compiling down or being processed by machine code. So, Eventually have to compile and do imperative things.

The idea of declarative code

  1. Easier to read and
  2. More productive

Leave a Reply

Your email address will not be published.