Programming Paradigms

Shivam
Analytics Vidhya
Published in
6 min readFeb 12, 2020
Photo by Markus Spiske on Unsplash

What is a Programming Paradigm?

Programming Paradigm is a style, technique, or way of writing a program. We can also say programming paradigms are the various approaches to solve a problem. And it is possible to solve the same problem using different paradigms, just like there can be multiple routes to reach from one village to another. However, certain types of problems fit more naturally to specific paradigms.

Why should we study it?

As we know, Programming Paradigms are different styles to write a program, hence knowing those styles and techniques will help us to choose the right paradigm based on the problem. I think every programmer should know the different paradigms, as learning a language is like learning the grammar or syntax of a particular language, but studying paradigms, on the other hand, are like learning the very principles upon which the languages are built. For example, when we learn the merge sorting, we don’t just learn the one more sorting technique. Instead, we learn the divide-and-conquer paradigm and the places where we can use this paradigm.

Almost all high-level computer programming languages support to write a program in one or multiple paradigms, and the decision to choose the right paradigm is left upon the programmer. For example, now, assembly language has features of high-level languages. We can write a program in assembly language in an object-oriented style.

There are two primary programming paradigms, an imperative and a declarative, and there are several paradigms that are influenced by these two types. I am going to cover all the major paradigms one by one. In this blog post, we are going to focus on both Imperative and Declarative paradigms. Let’s begin:

A computer program is a list of instructions that can be executed by a central processing unit. These instructions are in the machine language, in the binary form that is 0 and 1. But these instructions are not in the human-readable format; hence it isn’t straightforward to understand, which limits our ability to fix the bugs or modify the existing programs.

To overcome this problem, we created an assembly language that contained words from the English language. We call them mnemonic. Assembly language uses these mnemonic to represent each low-level machine instruction. Remember, the primary purpose of all programming languages (including assembly) is to provide an abstraction to the underlying machine architecture. The assembly language is easy to modify and understand in comparison to machine language. For example, we can write the machine code 10110000 01100001 as MOV AL, 61h in assembly, which means Load 97 decimal (61 hex) in AL register. MOV is an abbreviation of the move.

In the above program, we are informing the computer what to do next and also instructing to change its state(register values). This type of programming style is called the Imperative style of Programming.

The roots of imperative programming are rooted deep in computer architecture. The hardware implementation of almost all computers is imperative.

Imperative Paradigm:

The imperative style of programming emphasis on explicit control flow, it means the order in which statements, instructions, or function calls are executed. So the term explicit control flow means the programmer itself explicitly defines the order of execution of the instruction. The main characteristics of the Imperative style of programming are assignment statements and global variables.

Assignment Statements:

Imperative programming style uses statements that change a program’s state. The program state is nothing but the contents of the memory at a particular time. In computer programming, an assignment statement sets or re-sets the value stored in the storage location(s) denoted by a variable name. In most imperative programming languages like C, C++, Java, Kotlin, PHP, the assignment statement is a fundamental construct. For example, in the below code snippet, we are initializing the variables a, b, and total using the assignment operator (=).

Global Variable:

A global variable is a variable with global scope. Scope refers to the visibility of variables. By global scope, it means the variable is visible and accessible throughout the program.

Analogy:

I remember the imperative programming style using two analogies:

  1. Suppose you are in the restaurant, and while ordering your food, you don’t just choose the dish, but you also give instructions to the chef on how to prepare the dish.
  2. Consider a programmer as a King and computer as an army of that King. In the imperative style of programming king not only gives orders to do some actions to the military but also instruct how to execute those actions.

In short, the steps and sequences are explicitly given in the imperative type of paradigm.

Several paradigms are influenced by the imperative paradigm, such as Structural Paradigm, Procedural Paradigm, and Object-Oriented Paradigm. C, C++, Java, Kotlin, PHP are a few examples of languages that support the imperative paradigm. I will cover these paradigms in separate posts.

Declarative Paradigm:

Declarative paradigm is a non-imperative style of programming. In the declarative programming paradigm, we only tell the computer what the problem is and let the system decide what steps to take and also the sequence of those steps. This behavior is a contrast to an imperative style where we mention all the steps to solve the problem.

Imperative says how to do it, and declarative says what to do. In other words, declarative programming expresses the logic of a computation without describing its control flow. Following is the example of the declarative way of programming:

In the above example, we use query select name from vegetables; as an instruction to get information about the name of all vegetables, we didn’t tell the computer how to get the information. Instead, we just described what we want and not how to get it. This type of programming style is called as the declarative programming paradigm.
There are several paradigms and languages which are influenced by the declarative paradigm, such as a Functional Paradigm, Logical Paradigm, and Database. Prolog, SQL are few examples of languages that support the declarative paradigm. I will cover these paradigms in separate posts.

Imperative vs. Declarative paradigms:

In the Imperative paradigm, the programmer instructs the machine on how to change its state. In the declarative paradigm, the programmer merely declares properties of the desired result, but not how to compute it.

In the next two posts, we will look into the other types of paradigms, which are the form of these two main paradigms.

One note on the languages, some languages support programming in multiple paradigms, for example, high-level languages like Java, Kotlin supports writing program in both an Object-Oriented and Functional way.

Bonus:

I have created the following diagram to help me to understand the classification of the paradigms, let me know your thoughts on it.

We are going to cover other forms of Imperative and Declarative paradigms in detail in the following posts.

Imperative Paradigms:

Declarative Paradigms:

Thanks for reading this blog. I hope you enjoyed this post on the Programming Paradigms and learned something new. If you have any suggestions or questions, please add it in the comment below, Happy Learning 👏.

More From Me:

--

--

Shivam
Analytics Vidhya

Product Engineer @ Gojek. Likes to write on Productivity, Android App Development, Kotlin, Software Engineering, etc.