Declarative vs. Imperative Programming

Welcome to my blog ππ½
I'm a Front-End Developer with a passion for learning!
I write about Programming π©π½βπ» and Productivity Tips β
Search for a command to run...

Welcome to my blog ππ½
I'm a Front-End Developer with a passion for learning!
I write about Programming π©π½βπ» and Productivity Tips β
Hello Alyssa, Very informative article. I noticed a statement in the declarative example section.
"Below is an imperative way to take a list of musicians and create a new array of names."
I think you meant declarative way.
P.S Thanks for sharing
Good catch! Thanks for informing me of that typo. It has been fixed.
Loved this article.
I disagree with you in one point. I am more pessimistic and I think most programmers think in a imperative way because they were taught to be in full control and make premature optimization.
It is a pity but I hope this will change someday in the future. Imperative programmers are at more danger of being replaced with machines than declarative ones.
Hey Maxi, that is a fair point you make about programmers thinking in an imperative way.
Thanks for the reading the article and sharing your insight on the topic!
Although my experience is primarily with front-end development, my current job is affording me the opportunity to do more full-stack work. Naturally, this meant that I needed to explore the different database GUIs available on the market. Here is a l...

A Note on the Series: Fast Fridays π is a series where you will find fast, short, and sweet tips/hacks that you may or may not be aware of. I will try to provide these (fairly) regularly on Fridays. In this Fast Friday tip, I'll explain how to crea...

Over the past year, Iβve started incorporating a Git GUI into my workflow. I still reach for the terminal about 90% of the time, but Iβve found that a GUI can really shine when it comes to things like viewing diffs, checking stashes, or resolving mer...

Simplifying Branch Management with Git Worktree

With all the AI tools popping up these days, I figured I'd try out some speech-to-text software in an attempt to boost my productivity. I've been hearing a lot about βdeveloping at the speed of thoughtβ, so here's a list of the top speech-to-text sof...

If you've worked with functional programming at all, chances are you've heard the terms "declarative" and "imperative" at some point. These two terms are mental models, or programming paradigms, which is just fancy computer science jargon for saying it's a style of programming.
In this article, I will explain the differences between the declarative and imperative approaches as well as provide examples so you can draw comparisons.
Most definitions of imperative programming will tell you that it is code that describes how to achieve a certain outcome. For example, many low-level languages are imperative by nature since they need to know how to perform a list of instructions that the computer can execute.
Let's look at a non-programming example where the statement "pass me a glass of water" is a declarative phrase. If you were to imperatively ask someone to do this, it could sound something like this:
"Walk over to the table, grab the glass of water, pick it up off the table, walk about 15 feet and lastly hand me the glass."
In this contrived example, you're describing every action that the person needs to perform for you to get water. While the phrase "pass me a glass of water" is describing what the action will be. Both of these phrases will get the job done, however, one is more verbose than the other.
In contrast, declarative programming describes what to do and what should happen. As programmers, we have a natural tendency to think in a more declarative way.
Many front-end frameworks and higher-level programming languages provide an abstraction that allows programmers to write code in a declarative format. A benefit of this is that the code is easier for us to read and write.
One of the main reasons functional programming has become so popular is because it steers you in the direction of being more declarative. By composing functions and using them in conjunction with one another, we are declaratively telling the program what to do instead of how to do it.
Now let's look at some examples to drive the point home.
The OG for loop is typically used to provide an imperative example. for loops are good examples of an imperative approach because you have to write out exactly how the loop should operate and increment and whatnot. You are providing a list of instructions.
Below is an imperative way to take a list of musicians and create a new array of names:
const musicians = [
{
name: "Stan Getz",
instrument: "Tenor Saxophone"
},
{
name: "Dave Brubeck",
instrument: "Piano"
},
{
name: "Paul Desmond",
instrument: "Alto Saxophone"
}
];
const musicianNamesImperative = [];
for (let i = 0; i < musicians.length; i++) {
musicianNamesImperative.push(musicians[i].name);
}
//Output: [ 'Stan Getz', 'Dave Brubeck', 'Paul Desmond' ]
Notice how we are manually describing how the code needs to create a new array of names:
Next, we'll look at a declarative way to achieve the same result as the one above.
Array methods like map, filter, and reduce are good examples of a declarative approach because they describe what is happening (as opposed to how) and do not mutate state in any way.
Below is a declarative way to take a list of musicians and create a new array of names.
const musicianNames = musicians.map(musician => musician.name)
Besides being less code than the imperative approach, you can see that we are declaring the output we want. We're not concerning ourselves with the inner details of looping through and assigning as well as all the other things we had to manually program in the imperative example. All we have to do is pass a callback function with what we want and it will return back a new array with the desired output.
Declarative code will get compiled or processed by something imperative like machine code or an operating system. At the end of the day, the computer still has to know how something needs to be done. Eventually, the code has to compile into a format that does imperative things. Higher-level languages just provide a nice abstraction layer that makes it easier for humans to write code that will later be converted into something the computer can understand.
I hope this tutorial debunks the differences between the declarative and imperative paradigms. Adopting a declarative approach is a fundamental part of functional programming so understanding the differences between the two paradigms is a good thing to know!
If you enjoy what you read, feel free to like this article or subscribe to my newsletter, where I write about programming and productivity tips.
As always, thank you for reading, and happy coding!