Programming paradigms

Kunal Gaurav
3 min readSep 9, 2021

Imperative programming, declarative programming, functional programming, and object-oriented programming.

Today we will talk about various paradigms of programming. We will also present a few examples to help you better understand the topic.

A programming paradigm is a fundamental style of writing computer programs. There are mainly four programming paradigms:

  1. Imperative
  2. Declarative
  3. Functional (This is also considered as the subset of the declarative style)
  4. Object-Oriented

Imperative Programming

Imperative programming is a paradigm in which a series of statements are described as changing the state of each step. As a developer, you have to describe every single detail of the implementation with imperative programming. In other words, you tell the compiler step by step what you want to do. See the code below for an example:

Example for Imperative Programming

Here in this example, we are saying:

  1. Create an empty list result to hold the final result.
  2. Step through each number in the List numbers.
  3. Add it to the result collection if it is even.

Declarative Programming

Declarative programming is a programming paradigm in which you express the logic of a program without describing the step-by-step process (basically specifying what you want to do). See the code below for an example:

In this case, you just need even numbers from the original list of numbers, but you don’t need to specify how you want to do it. In this case, you need not worry about storing the results and how the loop is going to proceed.

The declarative style is most often the preferred programming method. The following are some of the advantages of declarative programming:

  1. The declarative style is almost like the natural language in general, which enhances the readability of your code.
  2. The compiler is managing most of the boilerplate code and you have to write fewer lines of code for the same task as a developer.
  3. You only need to worry about the final result and leave the details for the compiler to handle.
  4. Since the compiler abstracts the details of the implementation, it allows the compiler to make decisions to optimize the code.

Functional Programming

Functional programming is a programming paradigm in which functions are treated as the smallest component that maps values to other values. The data in the case of FP is immutable as the data which is passed to the functions cannot be changed, however, new data structures can be easily created. Unlike imperative programming in which statements update the running state of the program since FP works on immutable data and does not support the state. In FP functions can be passed as arguments to other functions or returned from other functions. Since FP has no mutable state, there are no side effects caused due to state-change. This is why parallel programming can be easily implemented using functions because independent units (functions) can easily run in parallel. Reusability and testability of functions also become very easy.

Object-Oriented Programming

OOP is a paradigm in which the program is organized based on the smallest component called objects rather than functions and logic. The object in OOP consists of the data in the form of attributes and behavior in the form of methods. Instead of logic, the main focus in the case of OOP lies around the manipulation of these objects. Usually, the building blocks of OOP are classes: which act as a blueprint for individual objects, objects: which are the instances of a class, methods: functions that describe the behaviors of an object, and attributes: represent the state of an object. OOP is mainly suited for large and complex systems.

--

--