Download Prospectus


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

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

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 Computer Science

Diploma in Artificial Intelligence

Alternatively, you can view all our online engineering courses here.

Recent Posts

What are the principles of operation of electrical cells?

What are the principles of operation of electrical cells? In our last article, we looked at the basic electrical parameters in D.C. circuits.  In this article, we’re going to move on to the principles of operation of electrical cells.  We’ll look at the different types of cells and batteries, their mechanisms, and their applications. What […]

How can we calculate basic electrical parameters?

How can we calculate basic electrical parameters? In our last article, we looked at how we can select the best engineering material for the task.  In this article, we’re going to move on to basic electrical parameters in D.C. circuits. As an engineer, we need to be able to define these key terms and apply […]

How can we select engineering materials?

How can we select engineering materials? In our previous article, we looked at elastic and magnetic hysteresis. In this article, we’re going to look at how we can select engineering materials.  In other articles, we have previously looked at the different materials and their properties.  However, it’s important that engineers understand how we can go […]