As a result of a previous post, I was asked to explain the difference between the declarative and imperative paradigms and if one is more efficient than the other. Thus, the easiest way to explain it that I can think of is with an example that I read in a book at university.
UIkit vs SwiftUI
Let’s imagine that we need a dress for a special event, such as a wedding.
For this we will have 2 options:
The paradigms could be described as these two options since both will reach the same goal, which will be to obtain that dress.
Now, in the first option, you could ask for one of your favorite size and color looking throughout the store for the alternatives that suit your styles or tastes. In the second option, you should make the decision of which fabric to use, which design you like or which suits you best and you should write down your body measurements.
How could it be likened to a programming paradigm?
Well, imperative programming specifies how it will be done by taking control of the process, while declarative programming specifies what will be done by providing computational logic.
The Declarative Paradigm: like SQL or Prolog, they ask you to specify “what to do” and they are in charge of converting it into code.
The Imperative Paradigm: As it should be, Pascal, Ruby or Java must be exhaustively described specifying “how to do” the task.
You can imagine that if my idea of buying a dress is to wear an emerald green dress and I find the exact color in a store, I can sacrifice the “exact model” of what I wanted for an approximate one that is already in a store. Where the importance is delegated to pre-established functions and it will accommodate me to the results that are proposed, but with the great comfort of knowing that I can always simply repeat my purchase.
If you wanted a dress completely true to what you imagined, you could not enter a store, you should ask a dressmaker to create a custom dress with the color and style with which you want it. Making the required design is in my control and carrying out step by step. While I have full control of each flow, I must remember each step making it a bit more difficult to write.
This, as you will realize, does not have a clear superiority, but it is about the results that we would like to obtain, but it is important to take into account its advantages when programming.
Main differences in terms of paradigms:
Imperative: It is described step by step with exhaustive instructions of what to do.
Declarative: Establishes the conditions that trigger an execution to produce the desired results.
Imperative: The emphasis on the flow of control can be easily followed through the process steps, but the more functions or processes you add to it, the more it will become long and complex, making it difficult and slower to read.
Declarative: It will avoid the step-by-step that characterizes the imperative paradigm making it less complex and requiring less code, as a consequence it will become much easier to read.
Imperative: It offers an easy way to customize each aspect since you have full control to adapt any aspect or structure according to your needs, although this can lead to more editing errors than declarative programming.
Declarative: Customizing code is more difficult due to syntax and dependency on pre-configured algorithms, and debugging will take longer due to stacks.
Imperative: It is more complicated to optimize because the tasks have much more code to process, the more code, the more likely it is to have errors.
Declarative: It is easily optimized because the algorithm controls the implementation, in addition, extensions can be added and updates can be made but it is possible to lose performance due to the greater need for memory and function calls.
Imperative: Long and complex but with more control
Declarative: Concise and precise but lacks details.
Conclusions
The imperative system gives more control to the user by giving more control to the compiler, the declarative system improves repetitive processes, that is, it facilitates the automation and expansion of systems.
The choice of which one to choose is subjective according to the problems you have at the moment, both will be equally capable of solving the same situations and equally recommended.
For tastes, it is a matter of colors, or in this case of what dress you want to obtain.