Understanding imperative and declarative programming
While we discussed programming paradigms in a previous article, the most common explanation of the difference between imperative and declarative programming is that imperative tells the computer how to do things, whereas declarative focuses on what we want to get from the computer.
![, Understanding imperative and declarative programming](https://ilearnengineering.com/wp-content/uploads/2023/07/imp_vs_decl.png)
Statements vs expressions
Imperative code uses statements and declarative code uses expressions. Expressions evaluate to a value, while statements tell the computer to do something.
In imperative programming, your code is based on statements that change the program state by telling the computer how to do things. In other words, your code is based on defining variables and changing the values of those variables.
In declarative programming, your code is based on expressions that evaluate their result based on the computer what you want.
![, Understanding imperative and declarative programming](https://ilearnengineering.com/wp-content/uploads/2023/07/statements_vs_expressions.png)
Let’s check out an example of declarative and imperative statements in JavaScript:
Declarative:
const expression = input => input.toLowerCase();
const expression2 = function(input)
{
return input.toLowerCase();
};
Imperative:
const statement = () => console.log(“hello world”);
const statement2 = function()
{
console.log(“hello world”);
};
Expressions focus on taking input and providing output while relying only on the input itself. This is called a pure function, as we saw above.
Statements don’t necessarily need any input or output; they can just call other functions or change some value somewhere outside of their internal state. When we talk about imperative code telling the computer how to do things, we mean that it focuses on creating statements that tell the computer how to do its thing. When we talk about declarative code focusing on telling a computer what to do, we mean working with expressions that map inputs into outputs.
Programming paradigms and programming languages
If you read our previous article on programming paradigms you read that some programming languages are suited to one type of paradigm, where others can be suited to more than one. Take JavaScript, the language from our example above. It is an amazing programming language that allows us to write code in different paradigms or combine them into a multiparadigmatic approach.
However, if we look at HTML, it’s a declarative language, and all of the instructions you provide when you use it follow that paradigm. You are telling the computer what you want to see, but you leave it to the deployment package to determine how it processes that. This makes HTML ideal for automation.
State Management
To fully understand the difference between declarative and imperative languages, we need to understand state management. The state is anything in your code that holds the current values of your system, for example:
let state = {
foreground: ‘#999999’,
background: ‘#FFFFFF’
};
const imperativeMakeBackgroundBlack = () => {
state.background = ‘#000000’;
};
//directly changes the state object outside its internal scope.
const declarativeMakeBackgroundBlack = state => ({…state, background: ‘#000000’});
//takes current state as its input and returns new state with changed value
//without changing the original state
Imperative code directly accesses the state and changes it, whereas declarative expressions never change the external state.
The challenge of state management in imperative code is that, with growing complexity, you may have many parts of the code touching the same state, and when your system starts having trouble it may be quite difficult to debug.
In declarative code, you focus on building a function composition that takes several small functions and strings them together so one function sequentially passes its output as an input to the next function in line.
Imperative programming is how all programming started and goes all the way back to Assembly in 1949 which built code based on changing the registry values that hold the current state of the application.
You can read more about imperative versus declarative from this article.
Interested in our courses?
Interested in computer engineering? Find out more about all the computer engineering courses we have available by clicking here.
Diploma in Computer Engineering
Diploma in Artificial Intelligence
Alternatively, you can view all our online engineering courses here.
Recent Posts
Kirchhoff’s current and voltage laws
Kirchhoff’s current and voltage laws In our last article, we looked at the principles and operation of a d.c motor. In this article, we’re going to investigate Kirchoff’s current and voltage laws, as well as how to apply them to engineering problems. Kirchoff’s law of current Kirchoff’s law of current states that the algebraic sum […]
What are the principles of operation of a DC electric motor?
What are the principles of operation of a DC electric motor? In our last article, we looked at the electrical parameters in series and parallel electrical circuits. In this article, we’re going to dive into the principles of operation of a DC electric motor. The motor effect When a current-carrying conductor is placed on a […]
What are the electrical parameters in series and parallel electrical networks?
What are the electrical parameters in series and parallel electrical networks? In our last article, we looked at the principles of operation of electrical cells. In this article we’re going to move on to the electrical parameters in both series and parallel electrical networks. When we have circuits with more than one resistor, we need […]