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

Understanding the mechanisms involved in a voltage amplifier

Understanding the mechanisms involved in a voltage amplifier In our previous articles, we discussed analogue signals and their characteristics.  We’re going to dive a little deeper into analogue signals and see how a voltage amplifier works. What is an operational amplifier? Operational amplifiers, or op-amps, are one of the basic building blocks of analogue electrical […]

Characteristics of analogue electronics

Characteristics of analogue electronics In our previous articles, we’ve discussed subjects such as the different types of semiconductors and how they work.  We’re now going to focus on analogue electronics—what it is and its characteristics. What is an analogue signal? An analogue signal is continuous; in other words, it can have an infinite number of […]

The structure and mechanism of a Bipolar transistor

The structure and mechanism of a Bipolar transistor In our previous article, we discussed the operation of metal oxide transistors, and now we’re going to have a look at bipolar transistors.  We’ll look at their mechanism and how they work. What is a bipolar transistor? A bipolar junction transistor is a semiconductor device which can […]