Imperative vs. Declarative Paradigms: A Tale of Two Travelers

Feb 19, 2025

Paradigm Comparison

Are you writing code like a tourist who follows a strict itinerary, or one who enjoys the journey and adapts?

When designing software, the way we express logic impacts maintainability, readability, and performance. Two fundamental paradigms—imperative and declarative programming—shape how we write code and query data.

To make this concept more relatable, let’s start with a story.


The Tale of Two Tourists: Raj vs. Sam

Raj and Sam, two best friends, took a trip to Paris. But they had very different travel styles.

Raj the Imperative Traveler

Raj planned every step:

  • 6:30 AM: Leave the hotel
  • 6:45 AM: Arrive at the Eiffel Tower
  • 7:00 AM: Take exactly 10 photos
  • 7:30 AM: Walk 1.2 km to Café Bonjour, order a croissant and espresso
  • 8:00 AM: Take Metro Line 1 to the Louvre, enter through Gate C, see the Mona Lisa first

His trip was efficient, but when his phone died (and his itinerary vanished), he was completely lost!

Sam the Declarative Traveler

Sam, on the other hand, had a different approach. He simply said:

“I want to see the Eiffel Tower, eat great French food, and visit the Louvre.”

He asked locals for a breakfast spot, stumbled upon a hidden bakery, and took his time at the museum. He even discovered an underground jazz bar Raj’s rigid schedule didn’t account for.

However, because he had no structured plan, he missed the last entry time for a special exhibition he really wanted to see.

So, Who Had the Better Trip?

The truth is, both approaches have value:

  • Raj’s imperative approach was great for efficiency and precision.
  • Sam’s declarative approach allowed for flexibility and discovery.

This balance of control vs. abstraction is exactly what we see in imperative and declarative programming.


Imperative vs. Declarative in Code

Imperative Approach (Step-by-Step Execution)

Focuses on how to perform a task with explicit control over execution.

Example: Sorting a List in Python Imperatively

def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]

arr = [64, 34, 25, 12, 22, 11, 90]
bubble_sort(arr)
print(arr)  # Output: [11, 12, 22, 25, 34, 64, 90]

Here, we manually define every step of the sorting process, making the code longer and harder to modify if a different sorting algorithm is needed.


Declarative Approach (Outcome-Oriented)

Focuses on what should happen, leaving execution details to the underlying system.

Example: Sorting a List in Python Declaratively

arr = [64, 34, 25, 12, 22, 11, 90]
sorted_arr = sorted(arr)
print(sorted_arr)  # Output: [11, 12, 22, 25, 34, 64, 90]

Here, we simply declare that we want the list sorted, without specifying how. The underlying system handles the logic.


Imperative vs. Declarative in SQL

Imperative SQL (Using Cursors to Fetch Data)

DECLARE @id INT;
DECLARE @name VARCHAR(100);

DECLARE cursor_example CURSOR FOR
SELECT id, name FROM customers WHERE country = 'USA';

OPEN cursor_example;
FETCH NEXT FROM cursor_example INTO @id, @name;

WHILE @@FETCH_STATUS = 0  
BEGIN  
    PRINT 'Customer ID: ' + CAST(@id AS VARCHAR) + ', Name: ' + @name;
    FETCH NEXT FROM cursor_example INTO @id, @name;
END  

CLOSE cursor_example;
DEALLOCATE cursor_example;

Here, we manually control how each record is fetched and processed.


Declarative SQL (Let the Database Handle It)

SELECT id, name FROM customers WHERE country = 'USA';

This simply states what we want, letting the database optimize execution behind the scenes.


When to Use Imperative vs. Declarative Approaches

Imperative vs Declarative SQL

Use Imperative When:

✅ You need fine-grained control over execution

✅ Performance optimizations require custom logic

✅ The problem is highly procedural (e.g., low-level algorithms)

Use Declarative When:

✅ Readability and maintainability are priorities

✅ The system (e.g., database engine, compiler) can optimize execution

✅ You’re working with high-level data transformations or functional operations


Conclusion

Both imperative and declarative paradigms have their place - just like structured and flexible travel plans each have their own advantages.

  • Imperative code is like Raj’s detailed itinerary - precise, controlled, and efficient but potentially rigid and exhausting.

  • Declarative code is like Sam’s flexible trip - easier to work with and full of discoveries but sometimes missing crucial details.

The best developers - just like the best travelers - know when to plan in detail and when to let the system take care of things.

Imperative vs Declarative SQL