Programming Paradigms (Styles)

Ali Shobeyri
Daresay
Published in
9 min readOct 24, 2023

Have you ever heard about some terms like “declarative programming”, “imperative programming”, “reactive programming” and etc.?

even if you haven’t heard about them you would probably use some of them before when you were writing codes without knowing that you were using them!

but what are they and why have you heard about them so much?

What is Programming Paradigms

Programming Paradigms are a series of principles, methodologies, and styles that specify structures and implementations of your software, also programming languages can be classified based on these paradigms.

we can classify these paradigms into two main groups :

  1. Imperative
  2. Declarative

Imperative programming vs. Declarative programming

According to the Cambridge Dictionary, we have these definitions for these two words :

Imperative (noun): the mood (= form) of a verb used for giving orders

Declaration (noun): an announcement, often one that is written and official

In Imperative Programming we care about the required steps to do a task and in Declarative Programming we care about the final result and not about the steps that are required to be done!

let’s take a look at these two codes (in Kotlin), I’m going to write a program that can calculate the sum of even numbers :

Imperative
Declarative

as you can see in the Imperative method I specify HOW I want to reach my target but in the declarative way, I just declare to the system WHAT I want and use the built-in functions from Kotlin.

so by now, you must know that the whole imperative and declarative can be summarized in this shape :

for instance, think about SQL, is SQL Imperative or Declarative? yes you’re right, it’s Declarative because in SQL you only say “I want these rows from table X which have Y and Z inside them, you don’t say HOW you want to reach your output but you only say WHAT you want

Imperative Paradigms

we have different types of Imperative paradigms but the most famous ones are :

  1. Procedural
  2. Object-Oriented Programming (OOP)
  3. Structured

Procedural Programming

In procedural Programming, we have a collection of procedures (functions (we have another term “Functional Programming”, it’s not the same thing)) and each one has a task, in order to generate our output we might use some of them.

There are many programming languages that can be counted as Procedural like C, C++, Java and etc, but here I’ll make an example of Pascal for you, the reason is in Pascal we have function and we also have procedure!

in Pascal, a procedure doesn’t have a return type and a function has, simple!

let’s do the same program we did for calculating the sum of even numbers :

you can see in procedural programming we defined a “procedure” called “CalculateSumOfEvenNumbers” and in order to get our desired output we must run this procedure (in this case we only have one)

procedural programming is really straightforward and I believe lots of code you’ve already written was in procedural style :)

Structured Programming

in Structured Programming we care about structured control flow, we have three concepts in Structured Programming :

Sequence: the code will be executed from top to bottom in a sequence that we write them (same as procedural actually)

Selection: it simply means if and else and switch-case, by if/else we can choose different flows to execute our code

Itraration: you already know them, loops! for, while, do while which prevents us from duplicating codes.

in a Structured way of programming, we try to divide the program into sub-systems (functions).

you already know lots of structured programming languages, C, C++, Java, Kotlin, and so on!

now you may ask what’s the difference between procedural and structured programming? they look exactly the same, well we can consider structured programming a subset of procedural programming with more features, in structured programming style we use modularisation and more advanced control flow.

let’s see another example, let’s write a program that can calculate factorial in both procedural and structured ways in Kotlin :

you can see in the first function we have a while loop that can calculate the amount of factorial, this function is counted as a procedure that we need to run to reach our output, but for the second function we have more control flow, if n is 0 or 1 then we don’t have to run a loop, also you can see we directly return the output and put “return” keyword after { so we can have a more structured way of coding.

I’d like to have this picture for procedural vs. structured, there’s no absolute procedural and no absolute structured, it’s more like a spectrum for me.

Object-Oriented Programming (OOP)

in some languages (like C) we have structure and we have object, structure (struct in C) is a way to group related variables into one place, in some other languages (like C++) we have another term which is Object (class in C++ and many other languages), an object is a struct that also has functions

let’s say like this, variables in an object are specifications of a real-world entity and functions are behaviors of that model! you have a Bird, the bird has a type (sparrow, canary and etc) and it can move and fly (functions or behaviors); also this bird is a kind of Animal, so we can have another class called Animal which has a type and move (it doesn’t have fly).

Object-Oriented Programming is the implementation of real word entities in code and it’s based on four concepts :

inheritance: a Bird is a kind of Animal, we can inherit some objects from other objects and borrow the specifications and behaviors of the parent class (with modification) and add extra specifications and behaviors

polymorphism: having multiple forms for behaviors that can be divided into dynamic (run-time) and static (compile-time), for example, we can have two functions with the same name but with different inputs for an object (static) or we can override a function of a parent object in child object (dynamic)

the reason that dynamic is run-time is sometimes we can have a code like this :

here the obj is from Parent type but we build it in Child form, so in run-time when we reach the doSth function the system will decide if it must call doSth from Child or Parent (it will call it from Child in this example)

but in static one :

the system will understand if it must call doSth() or doSth(input: Int) in compile-time

encapsulation: hiding data from other objects and restricting access to them only by setter and getter function :

the reason we would like to have a setter or getter is shown in the example, for instance here we don’t want the number to be a negative value, also maybe when some objects try to access the number we also want to an additional job to be done(for example here printing a log)

abstraction: it’s actually related to inheritance, we want to reduce complexity in objects, for example, think about that Bird class, we have another class called Animal which can be an abstract class and have some Initialization such as variables and functions (like move) and some abstract functions like eat (Bird will eat seed and Lion will eat meat) that must be implemented later in child objects but only defined in parent objects, we can not have an instance of this Animal class that is initialized with Animal constructor (we can have an Animal object which is initialized by Bird or Lion or …) because there’s no such thing as just an animal but every Bird and every Lion and … have some shared specifications that must be defined somewhere.

Like procedural programming, I believe you wrote LOTS of programs in this paradigm already :)

Declarative programming

we have different types of Declarative paradigms but the most famous ones are :

  1. Functional Programming
  2. Reacting Programming
  3. Logic Programming

Functional Programming

In functional programming, we care about mathematical functions (like f(x) = y), we have some key elements in function programming :

immutability: if you have a function and it has an input, that input must not be changed (so we must not use global variables)

recursion: don’t use loops, in order to create a loop you must use a recursive function, if you think about immutability this makes sense, for example, to create a sum of even numbers you normally create a sum variable out of the loop and you try to CHANGE it which is forbidden here (you always can mix one programming paradigm with another, so when I said forbidden I meant if you want to write 100% functional but this may never happen and shouldn’t happen too, you choose your paradigm based on the question and application)

here is an example in Kotlin, in order to calculate the sum of even numbers we used a recursive function in a functional programming style.

NOTICE: YOU MUST BE CAREFUL WITH USING RECURSIVE FUNCTIONS, normally they take more memory and for some expensive calculations you might get a crash and etc, you always must be sure when you need functional programming and when you need a different paradigm rather than functional

Higher-order functions: you can pass a function inside another function as input, like f(y) = z + 2 and y or f(x) = x² in math

Reacting Programming

reactive programming is mainly about events and asynchronous data streams, every time there’s a new event there will be a new stream of data and as the name implies, we will react to these changes.

reacting programming is based on the observable design pattern, in this pattern, there’s an observable that provides (emits) that data stream for us and there’s an observer that can subscribe to this observable and handle the result of the stream.

everything is asynchronous, meaning we can have multiple observables and multiple observers and there’s no blocker between them.

mostly when we write a program in reacting style we use a powerful tool called “reactive extension”, for example for Java developers we have RxJava and for C++ developers we have RxCpp.

Rx is a tool that can provide lots of extensions and different types of observables based on our needs and it’s a combination of reacting programming and functional programming, you can have multiple extensions for casting this flow of data or filtering them or combining them or etc.

here we want to calculate the sum of even numbers (again :)) in reactive style, we have a list of numbers and we define an Observable based on them, we will subscribe to that observable so every time we have a new input then we have a new emission, we use functional programming here as well (filter and reduce) and at the end, we have our final sum.

Logic Programming

The logic programming paradigm is based on formal logic, what is formal logic you would say? well, we have formal and informal logic, in formal logic we use symbols and in informal logic, we use natural languages (like English, Russian, Spanish, and so on), in formal logic, we have syntax and clear rules to reach a true or false statement and in informal logic, we care about quality.

for example, if you ask me “Is the weather cold today?” and I want to answer it in informal logic, I can say “It’s not that cold” or “It’s very cold” but in formal logic, I only can say “yes (true)” or “no (false)”.

In logic programming paradigms we have a set of rules, facts, and patterns and we can reach our final output based on these rules.

the most famous programming language for logic programming is Prolog, let’s see an example in Prolog, I want to write a program that has a set of rules for people with their relation (father and mother) and a set of rules of gender (male or female) and by passing two names to it I want to see what’s the relation between that two persons :

this program is divided into two Facts and Rules :

Facts I :

  1. John is Mary’s Parent
  2. John is Lisa’s Parent
  3. and so one

Facts II :

  1. John is Male
  2. Alex is Male
  3. Lisa is Male
  4. and so one

Rules :

  1. X is the father of Y if X is the parent of Y AND X is male
  2. X is the mother of Y if X is the parent of Y AND X is female

now if I run father(john, mary) the output is true and if I run mother(john, mary) the output is false

Any comment? => s.shb.s.ali@gmail.com

Also, you can find me on LinkedIn: https://www.linkedin.com/in/iryebohs/

--

--