Declarative Programming

Declarative programming emphasizes what result or relationship is desired rather than spelling out every execution step.

6.1 Core idea

Declarative programming emphasizes what result or relationship is desired rather than spelling out every execution step. The engine, compiler, framework, or solver determines how to evaluate the declaration.

Declarative approaches include SQL, regular expressions, CSS, configuration languages, logic programming, and many build and infrastructure tools.

Imperative versus declarative Text equivalent: in the imperative version the programmer writes the loop, the filter and the accumulation. In the declarative version the programmer states the desired result; the database's optimiser chooses the execution steps. The steps still happen — they are just not written by hand.

flowchart LR
  subgraph Imperative["Imperative: you write HOW"]
    I1[sums = empty map] --> I2[for each row] --> I3{active?} -->|yes| I4[add salary to its department] --> I5[repeat, then divide and sort]
  end
  subgraph Declarative["Declarative: you write WHAT"]
    D1["SELECT department, AVG(salary) ... GROUP BY department"] --> D2[query optimiser chooses a plan] --> D3[engine executes the steps]
  end

6.2 SQL as a declarative language

Output (SQLite via Python 3.13.1) on a sample employees table — Engineering (72,000 and 64,000 active; the inactive 90,000 is filtered out), Support (41,000, 47,000) and Research (58,000, 51,000):

The query describes filtering, grouping, aggregation, and ordering. The database optimizer chooses an execution plan. Exact Boolean and numeric syntax varies by database engine.

SELECT department, AVG(salary) AS average_salary
FROM employees
WHERE active = TRUE
GROUP BY department
HAVING AVG(salary) > 50000
ORDER BY average_salary DESC;
department | average_salary
Engineering | 68000.0
Research | 54500.0

6.3 CSS as declarative styling

The stylesheet declares styling rules. The browser resolves cascade, specificity, layout, and rendering. CSS is not a general-purpose programming language in the same sense as Python, but it illustrates declarative specification.

.course-card {
  display: grid;
  gap: 1rem;
  border-radius: 0.75rem;
  padding: 1.25rem;
}

6.4 Configuration and infrastructure as code

A configuration can declare desired state:

This is illustrative YAML, not a complete deployment manifest. A controller or deployment system may reconcile actual state with declared state. Configuration must still be validated and protected; declarative does not mean automatically safe.

service:
  name: course-api
  replicas: 3
  resources:
    cpu: "500m"
    memory: "512Mi"

6.5 Declarative versus imperative trade-offs

Expresses intent clearly Allows an engine to optimize execution Can reduce low-level control-flow code

Often easier to review as a policy or query Execution may be less obvious Engine-specific behavior matters

Debugging requires understanding the execution engine Complex declarations can become opaque

Exercise

Write a SQL query to find the three most popular courses. Then implement the same aggregation in Python and compare the responsibilities of application code and database engine.

Quiz

? In declarative code, who decides the execution steps?
- Nobody — declarative code has no steps. :: The engine still performs steps internally.
+ The engine, compiler, framework or solver that evaluates the declaration. :: For SQL, the query optimiser chooses a plan.
- The operating system scheduler. :: The scheduler runs processes; it does not plan queries.
- The programmer, line by line. :: That describes imperative code.

? Which are trade-offs of declarative approaches? (select all that apply)
+ Execution may be less obvious. :: You must understand the engine to debug performance.
+ Engine-specific behaviour matters. :: Dialects and optimisers differ.
- They can never be optimised. :: Leaving the "how" to the engine is what enables optimisation.
+ Complex declarations can become opaque. :: Large queries or configurations can be hard to read.

? Does declaring desired state in YAML make a deployment safe?
- Yes, declarative configuration is automatically safe. :: The module explicitly says it is not.
+ No — configuration must still be validated and protected. :: Declarative describes the style, not the safety.
- Only if it has fewer than 100 lines. :: Size does not determine sa
…