Skip to content

Declarative and Imperative Language

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.

Term Focus Typical question
Declarative The desired result What should be true?
Imperative The exact steps How should the machine do it?

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 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.

Terminal window
SELECT name
FROM users
WHERE score > 2

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)
Area More declarative More imperative
Database SQL query Manual row scanning
Frontend React component state to UI Manual DOM mutation
Infrastructure Docker Compose service definition Manual process startup commands
Data processing Pandas expression Manual loops over rows
  • 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.