5

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"?

6

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.

  • 9
    what the hell kind of eggs is that? – Ewan Apr 12 '18 at 17:41
  • 2
    @Ewan: 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 5:48
3

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

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

  • 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
2

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.

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"/>

Not the answer you're looking for? Browse other questions tagged or ask your own question.