Imperative - you instruct a machine what to do step by step. Example: assembly language.
Declarative - you instruct a machine what you want to get and it supposes to figure it how to do it. Example: SQL.
Imperative vs declarative is an example of limitation of categorical thinking. There is a spectrum of values, but we trapped with two categories.
Let's take a look at some examples.
List operations
List operations (iteration, filtering etc) from most imperative to most declarative approach:
- Imperative programming, which includes
goto
(orjump
in terms of modern assembly). For example assembly. -
Structured programming, which includes
for
loops and indexed access to elements of the array. For example Pascal. - Map/Reduce concept, which includes map and reduce operations, but abstracts away how list itself is organized. For example map/reduce in Scheme.
- Relational databases, which abstracts away all implementation details of storage. For example SQL.
CSS position models
CSS position models from most imperative to most declarative approach:
-
position
,float
,padding
,margin
. You need to juggle those properties to achieve the desired layout. For example, see how to position div in the center. - Flexbox. A lot of hacks went away, but you still can't describe layout precisely, it is still some approximation
- CSS Grid Layout. You can describe the layout.
Also, need to notice that there is a way too much "knobs" in CSS to manipulate layout. See this video from deconstructconf to understand how this task can be solved better.
Photo by Fabio Rose on Unsplash
Top comments (6)
Just wondering, if one were to imperatively code some functionality, and then expose its API under layers of abstractions, is it still considered declarative programming to use and implement said abstractions?
Yes. Otherwise declarative programming wouldn't exist at all, at least not in this world. Every code that is executed finally gets transformed into machine instructions which are imperative on a CPU with Von Neumann architecture. (All CPUs that are practically relevant today belong to this category.)
I would say yes. The question here is the separation of levels of abstractions. All modern computer-based systems are imperative deep inside, there is always the rock of Silica which takes imperative instructions, like to direct stream of electrons to right or left channel, right?
Relational databases consist of imperative loops and B-Trees inside, it's just implementation is hidden from you good enough so you don't have to think about it.
Or think about "goto considered harmful" paper. Goto is an imperative construct and Dijkstra wanted to remove it from high-level programming languages, but goto still exists in lower levels in form of
JMP
and it is ok. Loops, when compiled, are turned inJMP
instructions.Oooh, I just thought of something pretty interesting. Based on the answers above, is it controversial to classify HTML and CSS as declarative "programming" languages?
I see now. From the way you put it, I definitely agree that it is indeed a spectrum.