Imperative and Declarative Paradigm

Asked

Viewed 10,623 times

17

What are the differences between mandatory and declarative programming paradigms? Advantages and disadvantages?

2 answers

13


Briefly the imperative tells how to do and the declarative tells what to do.

Imperative

This paradigm is concerned with the details of the functioning of the algorithm 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 must have some support from others paradigms, as the imperative, even if in a very transparent way.

Much of the programming languages mainstream are more imperative. Even some who try to sell themselves as being from other paradigms, they only have certain characteristics of these paradigms. There’s practically no object-oriented language, purely speaking, even if their marketing says yes. Today some languages seek to have more declarative forms in addition to the imperative form.

With it you communicate better with the computer, says extamenet how to do.

Example of code C#:

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);

Declarative

This form can use a more fluent syntax. But not always. It is possible to use an imperative syntax and program declaratively. Often functional programming is considered a declarative form since it cares more about general execution than 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 of what one should do it is common to have ways to escape from the declarative form, perhaps even extending in some way the declarative style of encoding.

In this paradigm we have better abstractions and the language is considered of higher level.

Efficiency is not something expected in this paradigm. Not that it can not run something fast, in some cases may even get better results but in comparable codes, where you got as much optimization as possible in each paradigm, the declarative will be slower, even if the difference is not important.

Of course one can achieve even more efficiency in cases not optimized. The declarative form allows a good backend optimize the code in a way that the programmer has difficulty, precisely by not giving too many details, he can choose the best way. It is very difficult to improve imperative code. There are some things that can be done but you can’t completely change the way you do otherwise you can change the result. There are too many details.

To work, these languages usually need a backend which will interpret or compile the code. Probably written imperative.

Often declarative languages are not programming languages. They usually have limitations to store states, mainly intermediate ones, and in flow control structures, since this is not the goal 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 characteristics necessary to classify the language as 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. SQL default is not a programming language. With some extensions it may be.

C# has adopted LINQ which is a more declarative form of programming, including having a more imperative syntax and another more purely declarative syntax.

Other examples are the GUI declaration languages where you say how you want to mount the screen elements, with some form of XML or similar format, but do not tell how it should be executed. Other examples are HTML, CSS, Regex.

Programming languages may be more declarative, but not fully.

With the declarative you communicate better with humans.

Example of code C#:

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;
pares = pares.ToList();

Declarative form with imperative syntax:

var lista = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 };
var pares = lista.Where(num => num % 2 == 0).ToList();

I put in the Github for future reference.

The ToList() may be omitted in certain circumstances, but to be equal to the above example it has to be placed.

  • even the machine language is "compiled" in different ways within a chip... this makes this classification of imperative vs declarative somewhat varied according to the perspective of who sees or the context that applies. To say "the language C is declarative/imperative" means: "the language C is declarative/imperative" in relation to which?

8

Programación Imperativa

Imperative Programming is a state-based concept, defined by variables, and actions that are state manipulators, procedures. By allowing the use of procedures as structuring, also is known as, Procedural Programming.

Examples of languages: Pascal, C, Cobol, Python

Perks

  • Efficiency
  • Natural modeling of real-world problems
  • Market dominance
  • Well-established

Disadvantages

  • Focuses on "HOW" and not "WHAT" needs to be done
  • Difficult to legit

Explanation: In an imperative language, the programmer tells in detail the operations the program performs, including memory manipulation and direct interface to the program input/output.


Declarative Programming

The Declarative Programming, unlike the Mandatory Programming that informs the "COMO" computer of the instructions to be carried out, cares only to tell the computer "WHAT" needs to be done, it is up to the computer to decide which is the best solution for this request. Languages defined by this paradigm cannot be considered as programming languages, and yes, as sub-languages.

Examples of languages: SQL, XML

Perks

  • Easy database access (SQL)
  • Conversion of Complex Objects (Person, Employee) by Binding to Network Traffic (XML)

Disadvantages

  • Code illegality(When used functionally)

Explanation: When it says here usada de forma funcional, is referring to markup languages, such as XML and HTML for example, where it may be difficult to understand its content due to markup.


Source: Imperative Programming vs Declarative Programming

  • Hi @Pedro, why "Focus on "HOW" and not "WHAT" needs to be done" is seen as a disadvantage for PI (imperative paradigm)? E what it means to use P.D functionally?

  • excuse the delay, see the explanations in the answer if they clarify your doubt

Browser other questions tagged

You are not signed in. Login or sign up in order to post.