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:
- Go to the kitchen
- Grab the bread, peanut butter and a butter knife
- Remove 2 slices of bread
- Use the knife to butter one slice of bread
- Place the other slice on top of the buttered slice
- 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 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 is declarative programming. Our CSS classes are actually just defining how our elements should look while the browser takes care of the implementation.
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.
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.