Paradigm Deep Dive · 04 of 04

Declarative Programming

Describe what you want — let something else figure out how to make it happen. The paradigm of SQL, HTML, configuration files, and modern UI frameworks.

← Back to Foundations
Foundations

What Is Declarative Programming?

In declarative code you state the desired result; an engine, compiler, or runtime works out the steps for you.

Basic Concepts

  • Imperative vs Declarative: imperative = "do step 1, step 2…"; declarative = "this is what I want."
  • Engine / Interpreter takes the declaration and produces the steps automatically.
  • Idempotent & Composable: declarations describe an end state; running them again is safe.
  • Domain-Specific Languages (DSLs) are usually declarative — SQL, regex, CSS, Terraform.
  • Functional code is one form of declarative; declarative is the broader umbrella.
Imperative — How
// Filter even numbers (JS)
const evens = [];
for (let i = 0; i < nums.length; i++) {
    if (nums[i] % 2 === 0) {
        evens.push(nums[i]);
    }
}
Declarative — What
// Same thing
const evens = nums.filter(n => n % 2 === 0);
Flavors

Forms of Declarative Programming

Functional

Compose pure functions; no explicit control flow.

Logic Programming

Prolog, Datalog. State facts and rules; ask the engine queries.

Query Languages

SQL, GraphQL, SPARQL. Describe the data you want.

Markup

HTML, XML, JSON, YAML. Describe structure and content.

Configuration / IaC

Terraform, Kubernetes manifests, Ansible. Describe the desired infra state.

Reactive UI

React, Vue, SwiftUI, Jetpack Compose. Describe the UI for any given state.

Examples

Declarative in the Wild

SQL — Querying a Database
SELECT name, COUNT(*) AS orders
FROM customers JOIN orders ON customers.id = orders.customer_id
WHERE orders.created_at >= '2026-01-01'
GROUP BY name
ORDER BY orders DESC;

You don't tell the database how to scan tables, build indexes, or join rows — its query planner figures that out.

HTML — Describing a Page
<article>
  <h1>Welcome</h1>
  <p>This is a paragraph.</p>
</article>

No drawing instructions — the browser handles layout, fonts, line-wrapping, accessibility tree.

React — Reactive UI
function Greeting({ name }) {
    return <h1>Hello, {name}!</h1>;
}

You declare what the UI should look like for any name; React figures out which DOM nodes to add, remove, or update.

Terraform — Desired Infrastructure
resource "aws_s3_bucket" "reports" {
  bucket = "acme-reports-2026"
  versioning {
    enabled = true
  }
}

You describe the bucket you want; Terraform creates, updates, or destroys resources to make reality match.

Prolog — Logic & Inference
parent(tom, bob).
parent(bob, ann).

grandparent(X, Y) :- parent(X, Z), parent(Z, Y).

?- grandparent(tom, ann).
true.

State facts and rules. Prolog's engine explores them to answer queries — no algorithm code from you.

CSS — Styling
button.primary {
  background: #065A82;
  color: white;
  padding: 10px 20px;
  border-radius: 6px;
}

No drawing code — the browser engine maps your style declarations to pixels.

Trade-offs

Strengths & Weaknesses

Strengths
  • Concise — express intent without ceremony.
  • Easier to read & reason about; the "what" is right there.
  • Engines can optimize globally (e.g., SQL query planner).
  • Idempotent — re-running converges to the same state.
Weaknesses
  • Performance is the engine's responsibility — you can't always tune it.
  • Debugging is harder when the "how" is hidden.
  • Edges of the DSL push you back into imperative escape hatches.
  • Steep ramp when the abstraction leaks (esp. SQL, Terraform).
When to Use

Sweet Spots

Querying Data

SQL, GraphQL — the engine optimizes; you describe results.

UI & Layout

HTML/CSS, React, SwiftUI, Jetpack Compose, Flutter.

Infrastructure as Code

Terraform, Kubernetes manifests, CloudFormation.

Configuration

YAML/TOML/JSON for application and CI config.

Build Systems

Make, Bazel — declare targets; the tool figures out order.

Rule Engines / Workflows

Drools, BPMN, decision tables in finance & insurance.

Wrap-up

The Paradigms in Perspective

Real codebases blend paradigms. A typical web app uses:

  • OOP for the domain model (Customer, Order, Invoice).
  • Functional for transforming data (filter / map / reduce on lists).
  • Declarative for the UI (React/JSX), database (SQL), and infra (Terraform).
  • Procedural in the inner loops where every microsecond matters.

The skill is recognizing which paradigm fits which problem — and switching cleanly between them in the same codebase.

Continue

Other Paradigm Deep Dives