Ctrl K

Declarative and Imperative Language

Two ways of expressing computation: describing what result is wanted or describing the steps to produce it.

Declarative and imperative styles appear in programming languages, query languages, UI frameworks, data pipelines, and infrastructure tools. The distinction is practical because it affects how code is read, optimized, and maintained.

Overview

TermFocusTypical question
DeclarativeThe desired resultWhat should be true?
ImperativeThe exact stepsHow should the machine do it?

Imperative language

Imperative code describes the steps required to produce a result. It often uses explicit control flow, mutation, loops, and assignments.

This style is natural when the exact order of operations matters.

numbers = [1, 2, 3, 4]
result = []

for number in numbers:
    if number > 2:
        result.append(number)

print(result)

Declarative language

Declarative code describes the result more directly. The runtime, database engine, framework, or tool decides the execution details.

SQL is a common example. A query describes the data wanted, while the database decides the execution plan.

SELECT name
FROM users
WHERE score > 2

Same idea in programming

Even inside a general purpose programming language, code can be written in a more declarative style.

numbers = [1, 2, 3, 4]
result = [number for number in numbers if number > 2]

print(result)

Common examples

AreaMore declarativeMore imperative
DatabaseSQL queryManual row scanning
FrontendReact component state to UIManual DOM mutation
InfrastructureDocker Compose service definitionManual process startup commands
Data processingPandas expressionManual loops over rows

Common confusion

  • Declarative does not mean no execution happens. It means execution details are delegated.
  • Imperative does not mean bad. It is useful when detailed control is required.
  • Many systems mix both styles.
  • SQL is declarative, but database engines still execute detailed physical steps internally.