What is declarative and imperative programming? What are the pros and cons of using each?

What is declarative and imperative programming? What are the pros and cons of using each?

Tomek Skupiński's photo
Tomek Skupiński
·May 10, 2022·

6 min read

In computer programming, there are two main types of programming: declarative and imperative. Today we will take a closer look at them. After reading this article you will know the difference between them and familiarize yourself with some declarative and imperative code examples.

Imperative programming

First, we will take a closer look at imperative programming.

What is imperative programming?

In imperative programming, the programmer tells the computer what to do, step by step. This is in contrast to declarative programming, where the programmer only specifies what the desired result is, and leaves it to the computer to figure out how to achieve that result.

Imperative programming is the older and more traditional style of programming. It is sometimes referred to as "procedural programming". The most common Imperative languages are C and FORTRAN. However, any language can be used imperatively.

In an imperative program, the programmer writes a sequence of instructions for the computer to carry out. Each instruction specifies an action to be carried out, and may optionally provide information on how that action is to be carried out.

For example, in the JavaScript, the programmer might write a sequence of instructions like this:

let x = 0;

while (x < 10) {
    console.log(x);
    x++;
}

This code tells the computer to do the following:

  1. Create a variable named x, and set its value to 0.
  2. Enter a loop. The loop will continue as long as the value of x is less than 10.
  3. Inside the loop, the console log the value of x to the screen.
  4. Inside the loop, increment the value of x by 1.
  5. After the loop has finished, the program will terminate.

This is an example of imperative programming - we use step-by-step instruction to tell computers what to do. Let's look at declarative programming.

Advantages of imperative programming

Some of the advantages of imperative programming include:

  1. Easy to learn

    Imperative programming is often taught first because it is the simplest paradigm to understand. It is also the paradigm most similar to the way we think and speak. When giving instructions, we typically use an imperative tone: “Please make me a sandwich.” “Close the door.” “Take out the trash.” This paradigm is natural for many people, which is one reason why it is popular.

  2. Efficiency

    Imperative programming can be more efficient than other paradigms because the computer can do exactly what the programmer tells it to, and nothing more. If executed correctly this can lead to more streamlined and efficient code.

  3. Control

    With imperative programming, the programmer has complete control over the computer and what it does. This can be an advantage because the programmer can make the code do exactly what they want it to.

Disadvantages of imperative programming

As always there are some downs related to using this programming style:

  1. Hard to master

    Imperative programming can be more difficult than other paradigms because the programmer has to keep track of all the details of what the code is doing. This can lead to more errors and bugs in the code.

  2. Rigidity

    Because imperative programming is so specific, it can be inflexible and rigid. This can make it difficult to make changes to the code without breaking something.

  3. Limited Abstraction

    Imperative programming offers limited abstraction, which means that the programmer has to keep track of all the details of the code. This can make the code more difficult to read and understand.

Declarative programming

Now let's take a look at declarative programming and some code examples.

What is declarative programming?

In declarative programming, the programmer specifies what the desired result is, but leaves it to the computer to figure out how to achieve that result.

This is in contrast to imperative programming, where the programmer tells the computer what to do, step by step.

Declarative programming is the newer style of programming. It is sometimes referred to as "non-procedural programming". A good example of a declarative language is SQL. However, any language can be used declaratively.

In a declarative program, the programmer writes declarations of facts and/or rules. These declarations describe the desired result but leave it to the computer to figure out how to achieve that result.

For example, in SQL, the programmer might write a declaration like this:

SELECT * FROM employees

WHERE salary > 50000

This declaration says "retrieve all employees who have a salary greater than $50,000". It is the responsibility of the SQL engine to figure out how to carry out that instruction.

What is important here is that we specified the desired result, but not how to achieve it. This is really powerful as we can rely on SQL to handle it and focus on what we need.

Advantages of Declarative Programming

There are several advantages to using a declarative programming style:

  1. Declarative programming can make code more readable.

    When code is written in a declarative style, it can be easier to understand what the code is doing. This is because the code is written in a more natural language style, and the programmer does not need to worry about the details of how the code will be executed.

  2. Declarative programming can make code easier to maintain.

    Since declarative code is easier to read, it can also be easier to maintain. When code is easy to understand, it is also easier to modify and extend.

  3. Declarative programming can make code more portable.

    Code that is written in a declarative style is often more portable than code written in an imperative way. This is because the declarative style is less dependent on the details of the underlying platform.

  4. Declarative programming can make code more reusable.

    Code written in a declarative style can often be reused in other contexts more easily than code written in a procedural style. This is because the declarative style is more general and less dependent on the details of the specific problem being solved.

Disadvantages of Declarative Programming

Despite the many advantages of declarative programming, there are also some disadvantages to be aware of:

  1. Declarative programming can be more difficult to debug.

    Since declarative code is less specific about how the code will be executed, it can be more difficult to debug. This is because it can be difficult to trace the flow of execution through the code.

  2. Declarative programming can be less efficient.

    Due to the flexibility of the declarative style, it is often possible to write code that is less efficient than code written in a procedural style. This is because the procedural style can be more specific about the steps that need to be taken to solve a problem.

  3. Declarative programming can be less flexible.

    The declarative style can sometimes be less flexible than the procedural style. This is because the declarative style can be more constrained by the need to maintain readability and portability.

  4. Declarative programming can be more difficult to learn.

    The declarative style can be more difficult to learn than the procedural style. This is because the declarative style can be more abstract, and the programmer needs to have a good understanding of the underlying platform in order to use it effectively.

Wrapping up

In this article, we took a closer look and the imperative and declarative programming. Both styles have advantages and disadvantages. It is up to you to decide which style to follow, however, it is important to remember that most of the time you will need both in your projects. The key takeaway should be to learn to tell if a given code is written in an imperative or declarative way and what it means for you.

 
Share this