Declarative Code vs. Imperative Code

There are is a lot of discussion on the internet whether declarative code is superior to imperative code. This blog post will take a different perspective and show that it is not an either/or question.

Think about the first days of computing. If you wanted to use a computer you had to fully understand it. It was only useful to you if you spoke his language (machine code). In order to do the right thing the machine needed clear instructions from the programmer. The good thing was that you could fully control the machine but at the cost of complexity.

Computers still work the same today. We still need an imperative language to communicate with them in a proper way but we have abstracted away the complexity with additional layers such as Assembler and C. The price we paid for that is that we lost our ability to fully control the machine.

A question like “Is Declarative Code superior to Imperative Code” boils down to that:

ansible cluster_declarative a1 Declarative Code b1 Execution a1->b1 a2 Imperative Code b2 Execution a2->b2

Obviously the machine will not execute our declarative code. It will not even know what we are talking about. Therefore we should see it like that:

ansible cluster_declarative a1 Declarative Code b1 Imperative Code a1->b1 c1 Execution b1->c1

Declarative Code is just another abstraction layer that is build on top of some Imperative Code. It comes with the benefit of making some configuration easier but its cost is that we are losing some sort of control.

The right question should be: “When do we use Declarative Code and “when do we use Imperative Code.

“Finding good answers means asking the right questions first”