What are the differences between imperative and declarative programming paradigms? Advantages and disadvantages?
What are the differences between imperative and declarative programming paradigms? Advantages and disadvantages?
Essentially the imperative tells you how to do and the declarative tells you what to do.
This paradigm is concerned with the details of how the algorithm works and the declarative only with the correct semantics of what one wants to achieve. It is clear that it is very difficult to program only declaratively. The declarative should have some support from other paradigms, such as the imperative, even if quite transparently.
Most programming languages mainstream are more imperative. Even some that try to sell themselves as being of other paradigms, they only have certain characteristics of these paradigms. There is virtually no object-oriented language in fact, purely speaking, even if their marketing says yes. Today some languages try to have more declarative forms in addition to the imperative form.
C # code example:
var lista = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 };
var pares = new List<int>();
foreach(var num in lista) {
if (num % 2 == 0) {
pares.Add(num);
}
}
This form can use a more fluent syntax. But not always. You can use imperative syntax and program declaratively. Often functional programming is considered a declarative form since it is more concerned with general execution than the specific details.
The declarative form may be more expressive in some scenarios but it is very difficult to express details. Unless there is a limitation on what to do, it is common to have ways to escape the declarative form, perhaps even extending the declarative style of coding.
In this paradigm we have better abstractions and the language is considered of the highest level.
Efficiency is not something that is expected in this paradigm. Not that it can not execute something fast, in some cases it can even get better results but in comparable codes, where the maximum possible optimization has been achieved in each paradigm, the declarative will be slower even if the difference is not important. p>
Of course you can get even more efficiency in non-optimized cases. The declarative form allows a good backend to optimize the code in a way that the programmer has difficulty, precisely because it does not give many details, he can choose the best path. It is very difficult to improve imperative code. There are a few things that can be done but you can not completely change the way you do it otherwise you may change the outcome. There are too many details.
In order to work these languages usually need a backend that will interpret or compile the code. Probably written imperatively.
Many times declarative languages are not programming languages. They tend to have limitations in storing states, especially intermediaries, and in flow control structures, since this is not the purpose of this type of language. Without these characteristics, it is difficult or impossible to reproduce the Turing machine . It is possible to have in this paradigm the necessary characteristics to classify the language like programming.
SQL is a very declarative language. You specify how you want the result, but exactly how the database will find the desired result is its problem. Standard SQL is not a programming language. With some extensions it may be.
OC # adopted LINQ which is a more declarative way of programming, including a more imperative syntax and a more purely declarative syntax .
Other examples are GUI declaration languages where you tell how you want to mount the screen elements, with some form of XML or similar format, but it does not tell you how to do this. Other examples are HTML, CSS, RegEx.
C # code example:
var lista = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 };
var pares = from num in lista
where num % 2 == 0
select num;
Declarative form with imperative syntax:
var pares = lista.Where(num => num % 2 == 0);
Imperative Programming is a concept based on states, defined by variables, and actions that are state manipulators, procedures. Because it allows the use of procedures such as structuring, is known as, Procedural Programming.
Examples of languages: Pascal, C, Cobol, Python
Advantages
Disadvantages
Explanation: In an imperative language, the programmer tells you in detail the operations the program performs, including memory manipulation and direct interface to the program's input / output.
Declarative Programming, unlike Imperative Programming that informs the computer "HOWTO" the instructions should be executed, worries about just telling the computer "WHAT" needs to be done, It is up to the computer to decide the best solution for this The languages defined by this paradigm can not be considered as programming languages, but rather as sub-languages.
Examples of languages: SQL, XML
Advantages
Disadvantages
Explanation: When you say usada de forma funcional
, you are referring to markup languages, such as XML
and HTML
, for example, in which it may be difficult to understand your content because of markings .