Imperative programming: Overview of the oldest programming paradigm

Whether for app development, programming of machines, or the development of business software – the developer has to decide which programming language to use before the first line of code is written. There’s a wide range of programming languages available but each of them can be assigned to one of two fundamental programming paradigms: imperative programming or declarative programming. Both of these approaches have their advantages and disadvantages.

What are the characteristics of imperative programming languages? What weaknesses do developers need to consider? In this article, we answer the most common questions about the imperative paradigm.

What is imperative programming?

Imperative programming (from Latin imperare = command) is the oldest programming paradigm. A program based on this paradigm is made up of a clearly-defined sequence of instructions to a computer.

Therefore, the source code for imperative languages is a series of commands, which specify what the computer has to do – and when – in order to achieve a desired result. Values used in variables are changed at program runtime. To control the commands, control structures such as loops or branches are integrated into the code.

Imperative programming languages are very specific, and operation is system-oriented. On the one hand, the code is easy to understand; on the other hand, many lines of source text are required to describe what can be achieved with a fraction of the commands using declarative programming languages.

These are the best-known imperative programming languages:

  • Fortran
  • Java
  • Pascal
  • ALGOL
  • C
  • C#
  • C++
  • Assembler
  • BASIC
  • COBOL
  • Python
  • Ruby

The different imperative programming languages can, in turn, be assigned to three further subordinate programming styles – structured, procedural, and modular.

The structured programming style extends the basic imperative principle with specific control structures: sequences, selection, and iteration. This approach is based on a desire to limit or completely avoid jump statements that make imperatively designed code unnecessarily complicated.

The procedural approach divides the task a program is supposed to perform into smaller sub-tasks, which are individually described in the code. This results in programming modules which can also be used in other programs. The modular programming model goes one step further by designing, developing, and testing the individual program components independently of one another. The individual modules are then combined to create the actual software.

Declarative vs. imperative programming

Imperative programming languages differ from declarative languages on one fundamental point: imperative programming focuses on the “how”, declarative programming on the “what”.

But what does that mean? Imperative programming languages are composed of step-by-step instructions (how) for the computer. They describe explicitly which steps are to be performed in what order to obtain the desired solution at the end. By contrast, in declarative programming, the desired result (what) is described directly. This becomes clearer when using a cooking analogy for illustration: imperative languages provide recipes; declarative languages contribute photos of the finished meal.

In declarative languages, the source code remains very abstract in terms of the specific procedure. To get to the solution, an algorithm is used which automatically identifies and applies appropriate methods. This approach has numerous advantages: Programs can be written much more quickly, and applications are also very easy to optimise. If a new method is developed in the future, the abstract instructions in the source code mean that the algorithm can easily utilise the newer method.

Imperative programming example

Imperative programming languages are characterised by their instructive nature and, thus, require significantly more lines of code to express what can be described with just a few instructions in the declarative style. In the following example, the aim is to output a list of first names:

Imperative programming (PHP)

 

$participantlist = [1 => 'Peter', 2 => 'Henry', 3 => 'Sarah'];
$firstnames= [];
foreach ($participantlist as $id => $name) {
    $firstnames[] = $name;
}

Declarative programming (PHP)

$firstnames = array_values($participantlist);

Advantages and disadvantages of imperative programming languages

Many programming languages based on the imperative programming paradigm are in use today.

On the one hand, this is because the approach is the original form of programming. On the other hand, despite the existence of alternative models, the imperative paradigm still has some practical advantages.

The languages are relatively easy to learn, as the code can be read like a step-by-step instruction. Therefore, programmers normally learn an imperative language first as part of their training.

Easy legibility is a crucial factor in day-to-day operations. Ultimately, maintenance and optimisation of applications should not be linked to a specific person; different employees should be able to do it without too much difficulty even if they haven’t written the code from scratch themselves.

One disadvantage of procedural programming is that for more complex problems to be solved, the amount of code quickly starts to grow. It remains easy to read but becomes confusing due to its volume.

Execution is not clearly delineated from the programming as it is in the declarative style, therefore, subsequent interventions can produce unwanted errors. Extensions are also more difficult to implement in pure imperative code – unlike in the declarative paradigm, where there are methods that can be used to add them separately.

Advantages Disadvantages
Easy to read Code quickly becomes very extensive and thus confusing
Relatively easy to learn Higher risk of errors when editing
Conceptual model (solution path) is very easy for beginners to understand System-oriented programming means that maintenance blocks application development
Characteristics of specific applications can be taken into account Optimisation and extension is more difficult

In practice, mixed forms of the paradigms are generally used these days because both, imperative and declarative programming styles, have their advantages and disadvantages. However, the declarative programming style is becoming increasingly dominant, supplemented by imperative methods.


We’re all in this together. At IONOS we are
#hereforyou
Coronavirus is a challenge we need to face together.
We've created special offers on key products to help
your business keep going.
3 months free
Online Shop
Be where your customers are with your own online store.