Intro
These two terms are Everywhere! In many documents, blogs, videos, tutorials, and write-ups involving programming.
They refer to two ways programmers organize write their code and how they achieve tasks. One describes the how and the other the what.
The Imperative vs Declarative struggle is actually one about how we abstract and organize our code.
TL;DR
- ImperativeProgramming: is code that describes how to achieve a task, step by step, it’s code that describes how the machine (programming language not actually binary) is going to accomplish something.
- DeclarativeProgramming: is when a developer describes the task they wish to accomplish, the code is minimal and often comprised of SyntacticSugar doing the heavy lifting.
Imperative
Is the typical approach to accomplishing things, you write code that completes a series of steps to accomplish a task. E.g. looping through an array using a for-loop and doubling the elements, placing them into a new array.
function doobl(nums){
let doubles = [];
for(int i = 0; i < nums.length; i++){
doubles[i] = nums[i] * 2;
}
return doubles;
}
The function describes how the language will iterate through the array, doubling the elements and returning a new array. Our logic is shaped by the language’s mental model.
Declarative
This approach relies on interfaces, APIs, and other abstractions that declare what we want to do instead of writing out the explicit steps on how to do so…
Taking our previous example and re-writing it as a declarative.
nums.map((num) => num * 2);
This does the exact same thing, we abstract the looping portion of doobl
behind the map
function’s implementation.
Another example would be a simple SQL statement
select name, number from team where hairColor LIKE "%brown%";
It’s declaring what we want to retrieve from the DB while the execution steps (implementation) are hidden from us.
Another common example of declarative code is HTML. It’s code that describes what we want to render in the webpage, how the browser does isn’t our concern, most of the time.
The Illusion
The truth about declarative code is that it simply abstracts the implementation away from you. It’s for this reason, that most APIs and libraries are declarative, you don’t need to see their implementation.
Spotting Declarative vs Imperative Code
Can you trace the execution of each step? If something’s mostly/overly abstracted and hidden away then you’re likely looking at declarative code.
Here’s what to look for:
- High-level expressive syntax, that doesn’t go over every step. Including Syntax Sugar.
- Abstractions, how much of the work is hidden from you?
- State Management. Do you need to manage it yourself or is it handled by the language, framework, etc?
- Implicit control flow is a common for declarative expressions, where the flow is controlled on your behalf.
Which to Choose?
That’s up to your needs, goals, and tools. When working in a easy to follow context that isn’t ambiguous, try out some declarative expressions and statements.
When minute control is needed, it’s likely best to go the imperative approach to have more control over each step of the process.