focus photography of car shift gear

The 5 most popular Programming Paradigms every developer should know

If you are programming in one of these style: OOP which is class-base and real life object oriented emphasize on objects, methods or functional-style where pure function, and input, output argument is a core, you are doing one of a way called Programming Paradigms.

This article will explain to you what is programming paradigms are and how to apply each paradigm in your code style by example in Python.

Programming paradigm explain

A programming paradigm is a way or approach to programming that defines the methodology, principles, and techniques used to design and develop computer programs.

When reading here, you may have a question in your mind: “why having this kind of structuring my code-style? Isn’t just using one single style is enough?”.

My answer is that because there is no all-in-one way of code style that can match all project and fit to all the problem in so that multiple way of programming were introduce. Each type of paradigm support and solve some way of problems and so different projects solve many difference problems can easily have completely different code-style than others.

Another point is that by having a structured and organized code that base on specific kind of paradigm, it is easier for developers to write, maintain and understand their code which lead to deliver higher quality code and reduce the risk of having technical debt.

1. Imperative Programming

We will start by discussing the imperative programming paradigm, which is one of the oldest and most widely used paradigms. In this type of programming, a programmer will focus on how thing should be handle, step-by-step in a very specific way. In this style, programmer really think and handle the logic exactly how a machine will do and so he rely more on problem solving skill than what programming language can offer to him.

For example if you want to have sum of numbers from 1 to 10, the code in imperative-style will look like this:

sum = 0
for i in range(1, 11):
    sum += i

One of down side of imperative approach is that it works well for simple tasks, but as programs become more complex, it can become difficult to manage the program state and keep track of all the variables and their values.

2. Declarative programming

Next, we will cover declarative programming. In this approach, all the steps required to handle specific task are hide and developer only care about what the result is rather than how to make the result happen. By having this type of rule lead to more humanlike, readable code which reduce the effort of reviewing, debugging and reading the code.

In declarative programming, all the logic are hide inside abstracts and function that take arguments constructs and it calculate the result. Let’s apply declarative approach to program from section above and see the difference:

numbers = range(1, 11)
sum_of_numbers = sum(numbers)

Declarative programming is often contrasted with imperative programming, which focuses on the detailed steps required to solve a problem. While imperative programming is better suited for some problems, declarative programming can be more effective for solving complex problems that are difficult to express in a step-by-step manner.

3. Object-Oriented Programming

One of the most widely used programming paradigm, object-oriented programming (OOP) emphasize on the important of real life data by simulate it into how programmer think and solve problem. And so by thinking in a more humanlike way, coding in OOP often easier to read and maintain.

In OOP, every concept is display as “object” in which containing data and code. data including fields (or properties) and code is in the from of methods. To create an object, first programmer have to create class (you can think of it like a blueprint) and when you create an instance of class, it will be called object.

Below is an example for Python programming coded in OOP way that create a Person class that has name and age as properties and a say_hello method that do the logic print out name and age of that person:

class Person:
    def __init__(self, name, age): = name
        self.age = age

    def say_hello(self):
        print("Hello, my name is",, "and I am", self.age, "years old.")

person1 = Person("Alice", 25)

4. Functional Programming

Beside OOP, functional programming (FP) also one of the most used in programming world. Lot of popular framework and libraries out there follow the rule of FP in their codebase like: React, Redux, Ramda…

FP follow the concept of declarative programming in addition of having function as a first-class citizens which mean functions can be assign as variables, other function parameter or even can be returned from other functions.

One key concept in FP is that function should always treat as pure function, pure meaning that function should not having any side effect and only depend on input arguments to produce the final result. It is mean that once you input argument A into function B, the result returned from function B must always C.

A main difference between FP and OOP is that OOP emphasize on the idea of stateful, every object should have their own state and can be changed easily. While in FP, data should be immutable (or stateless), meaning it cannot be modified and FP encourage programmer to write high-order functions which take functions as input or return them as output, to perform operations on data.

In this section example, let’s see the use of map,filter and reduce are combined together to calculate the sum of the squares of the even numbers in a list:

from functools import reduce
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

even_numbers = filter(lambda x: x % 2 == 0, numbers)
squared_numbers = map(lambda x: x ** 2, even_numbers)
result = reduce(lambda x, y: x + y, squared_numbers)


5. Event-driven programming

Lastly, we will cover event-driven programming, which is an approach that focuses on responding to events, such as user input, key press or network messages, rather than executing code sequentially.

Event-driven is based on concept that having a main loop which is a loop that continuously listens for events and dispatches them to their corresponding event handlers (or callback function for short). When the callback function got triggered, an loop will be paused until the handler finished its work.

The most common used of event-driven programming is in graphic user interfaces (or GUI) like game, web application, embedded system. In these environment, user inputs and incoming messages normally unpredictable so by applying this paradigm, programs are able to response to events quickly and in a effective manner.

Most popular popular programming languages that support event-driven programming include JavaScript, Python, and Java.

Let’s create a simple GUI application in Python that’s consists of a label and a button. When the button is clicked, the text of the label changes to “Button clicked!”. We will use tkinder library for this example:

import tkinter as tk

class MyApp:
    def __init__(self, parent):
        self.parent = parent
        self.label = tk.Label(parent, text="Hello, world!")
        self.button = tk.Button(parent, text="Click me!", command=self.on_button_click)

    def on_button_click(self):
        self.label.config(text="Button clicked!")

if __name__ == '__main__':
    root = tk.Tk()
    app = MyApp(root)

By exploring the 5 most popular programming paradigms, I hope that this article has provided a useful overview of the different approaches to programming and the strengths and weaknesses of each. With this knowledge, developers can choose the best paradigm for a given problem and build better software.