Imperative vs Declarative Programming

02 / 11 / 2021

Read Time 10 minutes

Imperative vs Declarative Programming

The term ‘declarative programming’ has regained popularity in the recent years. But what exactly is it and how does it compare to imperative programming?

Wikipedia defines imperative programming as:

A programming paradigm that uses statements that change a program’s state. An imperative programming consists of commands for the computer to perform.

While declarative programming is defined as:

A style of building the structure and elements of computer programs—that expresses the logic of a computation without describing its control flow.

Okay so what do these definitions actually mean and how are they different? Let’s explore a simple analogy.

If I had a robot butler and I wanted it to make me a sandwich, imperatively I’d have to tell it this:

  1. Go to the kitchen
  2. Grab the bread, peanut butter and a butter knife
  3. Remove 2 slices of bread
  4. Use the knife to butter one slice of bread
  5. Place the other slice on top of the buttered slice
  6. Bring me the sandwich

To do the same thing declaratively would look like this:

“Hey Mr Robot, I feel like a peanut butter sandwich.“

Quite the difference, right? Imperatively means that we have to tell our robot butler (the program) every step it needs to do in order to complete the task. Doing the same thing declaratively means that we just declare our final state (having a sandwich) and we don’t really care how it gets to that point.

We can now see how declarative programming is essentially just stating what you want and then the runtime system has the responsibility of making that happen.

This means that declarative programming is generally simpler to code because it’s far more readable, doesn’t require concerning yourself with lower level logic and allows use to define the solution and not each procedure.

You may have been writing declaratively without even knowing it:

SQL

SQL queries can also be declarative.

Here we are just saying that we want all of the games in our database but we aren’t instructing the computer on how it should be getting every game.

CSS

CSS is declarative programming. Our CSS classes are actually just defining how our elements should look while the browser takes care of the implementation.

C Sharp

C# allows for developers to write both imperatively and declaratively.

The above code is an example of how, using Linq, we can write declarative code. All we are asking for is a list of games that are a part of the “God of War” series but we don’t care how the program gives us that data. That’s Linq’s problem.

Achieving the same functionality imperatively would look like this:

Okay so why has declarative programming become more popular in recent years? Well that would be because of React.

React has heavily promoted itself as declarative. The reason for this is because alternative frameworks (like Angular) or libraries (like jQuery) are imperative. React allows us to write the code that we want while it worries about taking that code and performing any steps that are required on the JavaScript/DOM side of things. This may leave you wanting a code example but the difference between React and Angular is a blog post on it’s own.

Writing less code with regards to your UI rendering does come at a cost in React though. You’re going to need to now understand state and how to manage it so that you can effectively render your UI.

Hopefully now you have an idea on what these concepts are so the next time some hot shot coder is throwing around terms like “paradigm” and “declarative programming”, you can ask them how their robot butler is doing.

View
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website.

These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.

Necessary

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Non-Necessary

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.