I found it easier to distinguish between declarative and imperative based upon idempotent and commutative. Use the references to know about them.
Checkout this simplified version to know about idempotent.
Then I bring in the definition of "WHAT" & "HOW" to understand what "WHAT" & "HOW" actually mean. In declarative, you connect one data with another by defining a relationship between them. You don't mention how that relationship should be achieved rather "WHAT" that relationship is. Through a relationship you describe "WHAT" your output data looks like rather than "HOW" to achieve this output data.
Start drawing some diagrams in our head, draw some dots (data) and connect them with lines (relationship). Draw in all possible ways one to many, many to one & one to one. Give arrows to these lines, like this <-----------. All arrows should be facing left because all datas that a particular data is based upon must be calculated first and then move left to calculate that particular data.
If data a is based upon data b, data c and data d which in turn might be based upon on some other datas. Then b, c and d should be calculated first and only then a will be calculated. So a is on the left side of line and all others on the right. There will be 3 lines reaching a one from each of b, c and d.
This diagram has some properties:
- NO data will violate the relationship it has with all other data
- control flow or the order doesn't matter, of course
b, c and d should be calculated before a but there is no preference between b, c and d i.e. it doesn't matter which one of these 3 is calculated first (commutative)
a is only based upon b, c and d and no one else. Hence, it doesn't matter how many times the relationship operation that calculates a using b, c and d is executed, same a should be achieved (idempotent). a is the end result of the relationship operation here. Basically, everyone who is affecting a should have a line pointing to a.
These relationships (lines) are like functions (functions of Mathematics and NOT programming). No doubt functional programming is famous among people of academia. Pure functions (of our programming, therefore not in bold) are like functions (of Maths, therefore in bold).
By now declarative might have started to sound like PURE and IMMUTABLE (which are generally used in Functional Programming) to you, if yes GOOD and if no GREAT. Because that's not the aim here, that's something that automatically emerged out of this pattern.
If your piece of code can be converted into this diagram then it's completely declarative otherwise, it lies somewhere else on the scale.
Declarative is close to Maths.
Now let's zoom in into these relationships (lines), to see what's going on inside the computer during program execution.
Imperative comes in. Here's where that ground-level work is done. In imperative, you mention step by step "HOW" it needs to be done and you know that this sequence of steps will create the requested relationship between one data (inputs b c d) and another data (output a). Here you create variables, mutate them, loop through array and all other things.
Imperative is close to Programming.
Instead of saying a program to be declarative or imperative, I prefer to see it on a scale where on the leftmost side I have completely declarative and on the rightmost side it's completely imperative. Remember, declarative is built on top of imperative, hence any declarative thing you see is actually imperative underneath. Generally, programs are a mixture of declarative and imperative.
Now, let's take these 2 examples:
Second example can be converted to the diagram like this:
reduce_r map_r filter_r
a <--------- b <--------- c <--------- d
- filter_r (relationship):
c is only even numbers of d
- map_r (relationship):
b is all numbers multiplied by 10 of c
- reduce_r (relationship):
a is all numbers added of b
this should look like compound function of maths: reduce_r( map_r( filter_r( d ) ) )
In declarative, the job of developer is to break the ultimate goal (a) into subgoals (b, c) which will help to reach the ultimate goal.
of course under the hood of procedures map, reduce and filter is imperative code running.
Food for thought: if you are required to make an assumption on map function to go from left to right to make your code work as expected, you are actually doing imperative in the name of declarative.
References: purpleidea (James), www.dataops.live, wiki.c2.com