7

Is it possible to say that the difference between imperative programming and declarative programming is merely in the level of abstraction? I.e. where the imperative programming will say "break the eggs, put them into plate, turn the oven to 180 degrees, etc", a declarative language will only say "cook me eggs"?

Robert Harvey
  • 199,517
  • I feel like this question must have been asked before, but much to my surprise, I can't find a duplicate. – Jörg W Mittag Apr 12 '18 at 12:36
  • 3
    Ah, that's because those duplicates were asked before [softwareengineering.se] existed: https://stackoverflow.com/q/602444/2988 https://stackoverflow.com/q/1784664/2988 – Jörg W Mittag Apr 12 '18 at 12:46
  • @JörgWMittag When you remember you've already answered the question and can't find your own answer :') – Christopher Apr 12 '18 at 13:02
  • 1
    Here's another one: https://stackoverflow.com/questions/1784664/what-is-the-difference-between-declarative-and-imperative-programming – Martin Maat Apr 12 '18 at 13:22

4 Answers4

7

Declarative languages are not necessarily higher abstraction level. Thay are not magical - they cannot figure out on its own how to cook eggs if you dont tell it how (unless there is a library for it, but that would be the some for imperative). You still have to provide the same information as an imperative program would need. The difference is in how the information is expressed.

Imperative: A sequence of steps (possibly with branches, loops etc.). "break the eggs, put them into plate, turn the oven to 180 degrees, etc",

Declarative: The end goal, described in sufficient detail: "eggs which have been placed (without shells) in an oven on a plate at 180 degrees"

A more realistic example would be how to draw a line:

Imperative:

graphics.setPen(Color.Red, Size.Big)
a = new Point(100,100);
b = new Point(200, 200);
graphics.drawLine(a, b);

Declarative:

Line:
  start: Point(100,100)
  end: Point(100,100)
  color: Red
  size: Big

The abstraction level is more or less the same (the concepts - line, point, colors - are the same, and the same amount of information is given), but the imperative is expressed as a sequence of steps, while the declarative express the end goal.

In practice though, the most well known declarative languages are high-level domain specific languages like SQL, CSS, HTML etc, while lower level general-purpose languages like C, Java etc. are generally imperative. It appears declarative is more suited to high level DSL, while imperative is more suited to general purpose programming.

JacquesB
  • 59,334
  • 9
    what the hell kind of eggs is that? – Ewan Apr 12 '18 at 17:41
  • 2
    @Ewan: https://www.allrecipes.com/recipe/232024/hard-boiled-eggs-in-the-oven/, though I think you're going to need 350 degrees. – Robert Harvey Apr 12 '18 at 18:18
  • 2
    @RobertHarvey - here in Europe, we tend to use degrees Celsius, not Fahrenheit, so the only common type of oven that even goes up to 350 is clay firing ovens. – Jules Apr 12 '18 at 19:55
  • 1
    @Jules: 350 degrees F is 177 degrees C, so I guess you're right. – Robert Harvey Apr 12 '18 at 19:58
  • Downvote because of the cooked eggs= part. That is not declarative. Declarative is cooked egg=egg + proteins denaturalized + no liquid components left – marstato Apr 13 '18 at 05:48
  • I agree that HTML (e.g. <!DOCTYPE html><html lang=""><title>x</title>) and CSS (e.g. h1 { font-size: 3rem; }) are declarative languages since they express results. But aren’t SQL (e.g. INSERT INTO relation (x) VALUES (3);) and HTTP (e.g. PUT /path HTTP/1.1 {"x": 3}) imperative languages rather since they express commands (like the assignment command x = 3; in C)? – Géry Ogam Jul 19 '21 at 23:01
  • 1
    @Maggyero: Yes only the query expression parts of SQL can be called declarative, the data modification and data definition parts (CREATE TABLE ... etc.) are imperative. Some SQL versions also have imperative extensions like loops. – JacquesB Jul 20 '21 at 09:26
  • I see thanks. But I wonder if query expressions in SQL are not interrogative rather than declarative. I have just opened a separate question here if you are interested. – Géry Ogam Jul 20 '21 at 11:06
  • @Maggyero: "Imperative" have a particular meaning when discussing programing languages, which is not the same as when discussing natural languages. – JacquesB Jul 20 '21 at 12:03
4

No, the declarative program would say "cooked eggs".

Just where you want to go, not how to get there. Just a result, no commands.

Martin Maat
  • 18,435
  • 2
    I feel like this doesn't really capture the essence of declarative programming. Nobody writes functions that say CookedEggs(), though you could store the result in a variable called cookedEggs, and there is much more to declarative programming than merely stating the product. – Robert Harvey Apr 12 '18 at 15:21
  • 1
    @Robert Harvey When I think of declarative programming I think documents, not code. – Martin Maat Apr 12 '18 at 15:34
  • 5
    SQL is a declarative programming language (more or less). Linq is more declarative than loops. – Robert Harvey Apr 12 '18 at 15:38
4

This is true in one "direction," but not both.

Declarative programming is always some abstraction layer(s) above imperative programming (Assembly is always imperative). You are just letting some other function (an abstraction) figure out the imperative steps, by giving it a state description (a declaration).

But adding an abstraction layer above imperative programming does not always produce declarative code.

A simple proof:

Addition is a base concept.

Multiplication of whole numbers is just repeated addition (multiplication is an abstraction of addition).

But 4 * 12 is still an imperative expression.

Matias
  • 41
  • Declarative is not necessarily higher level, for example a HDL (hardware description language) can be declarative, which is lower level than assembler. – JacquesB Jul 20 '21 at 12:53
0

Declarative programming is basically hiding chunks of implementation under the hood and providing compose-able constructs to use these implementations... creatively.

Think of HTML & CSS, the browser "knows" the imperatives/commands/steps for rendering CSS rules and tags, in domain of drawing stuff on screen.

Rather than Abstraction, its more of an Encapsulation technique:

<egg class="broken oven-cooked medium"/>
S.D.
  • 1,080