Declarative vs imperative. What does it mean to write declarative or imperative code?

Hemant
3 min readMay 21, 2023

--

Declarative and imperative are two contrasting programming paradigms that describe different approaches to writing code. Declarative programming is a paradigm describing WHAT the program does, without explicitly specifying its control flow. Imperative programming is a paradigm describing HOW the program should do something by explicitly specifying each instruction (or statement) step by step, which mutates the program’s state.

Let’s explore the differences between declarative and imperative code and provide examples for better understanding:

Declarative Code:

Declarative code focuses on describing what needs to be achieved without specifying how it should be done. It emphasizes the end result rather than the step-by-step instructions. In declarative programming, you define the desired outcome, and the underlying system figures out the most efficient way to achieve it.

Example 1 — CSS:

/* Declarative example */
.button {
background-color: blue;
color: white;
font-size: 14px;
}

In this example, the CSS code declares the desired styles for a button. It specifies the desired background color, text color, and font size, without specifying the exact steps to apply those styles.

Example 2 — SQL:

SELECT * FROM users WHERE age > 18;

In SQL, you declare the desired data you want to retrieve using a query. In this example, you’re declaring that you want to select all the records from the “users” table where the age is greater than 18.

Imperative Code:

Imperative code focuses on explicitly defining the steps or instructions required to achieve a specific outcome. It describes how the task should be performed, often involving explicit control flow and mutable state.

Example 1 — JavaScript:

/* Imperative example */
let numbers = [1, 2, 3, 4, 5];
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
console.log(sum);

In this JavaScript example, you explicitly iterate over the array numbers using a for loop and keep adding the values to the sum variable.

Example 2 — Procedural Programming:

Copy code
# Imperative example (Python)
def calculate_sum(numbers):
sum = 0
for num in numbers:
sum += num
return sum

numbers = [1, 2, 3, 4, 5]
result = calculate_sum(numbers)
print(result)

Here, a procedural programming approach is used, where a function calculate_sum explicitly defines the steps to calculate the sum of numbers in the given list.

Key Differences:

  1. Focus: Declarative code focuses on what needs to be achieved, while imperative code focuses on how to achieve it.
  2. Control Flow: Declarative code relies on the underlying system or framework to handle the control flow, whereas imperative code explicitly defines the control flow.
  3. State Mutation: Declarative code tends to avoid or minimize state mutation, whereas imperative code often involves mutable state and explicit modifications.
  4. Readability and Maintainability: Declarative code can be more readable and easier to maintain as it focuses on the desired outcome, while imperative code can be more verbose and require an understanding of step-by-step instructions.

It’s worth noting that in practice, many programming languages and frameworks incorporate both declarative and imperative elements, allowing developers to utilize the most appropriate approach for a given task or context.

# Imperative example (Python) def calculate_sum(numbers): sum = 0 for num in numbers: sum += num return sum numbers = [1, 2, 3, 4, 5] result = calculate_sum(numbers) print(result)

--

--

Hemant

Passionate Developer on a continuous quest for innovation. Let's explore the digital frontier together.