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"?
-
I feel like this question must have been asked before, but much to my surprise, I can't find a duplicate.– Jörg W MittagApr 12, 2018 at 12:36
-
3Ah, that's because those duplicates were asked before Software Engineering existed: stackoverflow.com/q/602444/2988 stackoverflow.com/q/1784664/2988– Jörg W MittagApr 12, 2018 at 12:46
-
@JörgWMittag When you remember you've already answered the question and can't find your own answer :')– ChristopherApr 12, 2018 at 13:02
-
1Here's another one: stackoverflow.com/questions/1784664/…– Martin MaatApr 12, 2018 at 13:22
4 Answers
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
-
2@Ewan: allrecipes.com/recipe/232024/hard-boiled-eggs-in-the-oven, though I think you're going to need 350 degrees. Apr 12, 2018 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.– JulesApr 12, 2018 at 19:55
-
1@Jules: 350 degrees F is 177 degrees C, so I guess you're right. Apr 12, 2018 at 19:58
-
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.– JacquesBJul 20, 2021 at 9:26
No, the declarative program would say "cooked eggs".
Just where you want to go, not how to get there. Just a result, no commands.
-
2I 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 calledcookedEggs
, and there is much more to declarative programming than merely stating the product. Apr 12, 2018 at 15:21 -
1@Robert Harvey When I think of declarative programming I think documents, not code. Apr 12, 2018 at 15:34
-
5SQL is a declarative programming language (more or less). Linq is more declarative than loops. Apr 12, 2018 at 15:38
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.
-
Declarative is not necessarily higher level, for example a HDL (hardware description language) can be declarative, which is lower level than assembler.– JacquesBJul 20, 2021 at 12:53
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"/>