Introduction to Programming Paradigms
By: Osgood Gunawan
Tags: Software Engineering, Programming Paradigms, Software Development, Computer Science, Programming

Photo by Clément H on Unsplash

As someone who never graduated as a computer science degree but very passionate and eagerly wants it to know more about programming as a whole, you will eventually encounter different hurdles of programming subjects that you are unfamiliar with. In my case, “paradigms” in programming have been the topic that stuck in my brain for a while. The best way to learn and understand a new topic is to test yourself to teach/write an article/make a video of that topic for someone else who is also trying to learn. So here, I am going to talk briefly about the programming paradigms.

First, what is a paradigm? A paradigm is a pattern of something or a model. In the programming term, it is essentially the approach to programming that language support. In other words, programming paradigms are just different styles or ways of programming. The paradigms models that we use also defines the task and means of programming. There is a mass of programming paradigms, but I am here mainly talking about the two main ones, declarative vs. imperative.

The principal programming paradigms by Peter Van Roy

Imperative Programming Paradigm

An imperative Programming paradigm is usually how the average person thinks of programmers. Programmers in this paradigm give an order or command on “how” the computer should explicitly execute, and mostly it performs from top to bottom lines of codes. The order of the steps is crucial because a given step will have different consequences depending on the current values of variables when the action executes. The time and state are matters since it is mutable and exact details of steps(precision matters). Think about it as a clock(time machine), every second-minute hands and dials are a matter to the time.

Another example would be going to a restaurant ordering a burger. You request that burger by commanding the waiter to have a slice of medium-rare meat, the fries need to add extra salt and fried more crispy, the lettuce should be only two slices, tomatoes three installments, and lastly, add more bbq sauce and pickles on the top of the burger. As you can see, everything is an exact precise order. With this in mind, there are two main sub paradigms in imperative programming.

Procedural programming: It is a concept based on routines, subroutines, modules methods, or functions(procedures). It is like a list of instructions to tell the computer what to do step by step(procedures). It is also known as top-down languages. Most of the early programming languages are all procedural.

-Programming languages: Cobol, C, C++, Java, Pascal.

Object Orienting Programming(OOP): Treat everything as an object passing messages to one another. It can share properties or behaviors as well as changing state and reusable components. Each viewed as separate entities having their state, which is modified only by built-in procedures, called methods. Lastly, it is the most widely used paradigm around the world; it is easy to understand and read.

-Programming languages: Java, Ruby, C++, Python, Javascript.

Declarative Programming Paradigm

A declarative programming paradigm is a style of building programs that express the logic of a computation without talking about its control flow(Wikipedia). Instead of telling the program “how” to do but “what” to do. A better way to understand this, we can go back to the example of ordering a burger in the restaurant. This time you are just going to order one big mac without even mentioning a lot of precise details to add the burger. You are telling them what you are going to order, as opposed to how you are going to order the burger. Since the declarative paradigm expressing logic computational, it is used a lot in mathematical logic terms. There are two main sub paradigms in declarative programming.

Logical Programming: Based on mathematical logic, facts, and rules within the system (not instructions).

-Programming languages: Prolog, ALF (algebraic logic functional programming language), Ciao, Alice.

Functional Programming: a programming paradigm that treats computation as an evaluation of mathematical functions, avoiding changing-state and mutable data. No side effect and more comfortable to debug. It is taking some inputs from whatever arguments, and it returns the output value (data goes in, and data comes out). The functions do not modify any values outside the scope of that function, and the functions themselves are not affected by any values outside their scope.

-Programming languages: Haskell, Kotlin, Scala, Clojure, Elm, Mercury, Javascript.

The differences between the two programming paradigms

Notice some of the programming languages overlaps declarative and imperative programming paradigms. Some of these languages have both declarative and imperative programming paradigms, which we called multi-paradigm programming languages. It is basically if the programming language has more than one paradigm, we called them multi-paradigm programming languages. However, some functional programming languages are purely functional, for instance, Haskell, Elm, and Mercury. Some have both paradigms such as Javascript, Java, C++, Scala, Python, Kotlin, Rush.

Example of code in imperative vs. declarative programming

“Imperative programming is like how you do something, and declarative programming is more like what you do.”

There is no the best paradigm, is all depends on which scenario is your task for and what type of problems it makes sense to solve. Each model has its best for an individual case. Paradigms define programming, just like paradigms enable scientific progress. As a community, we need to agree on a particular model of programming and what it means to write a good program for us to get anything done.

This article intended to be a brief introduction to the programming paradigms. If you want to know more about programming paradigms in-depth, you should check out these fantastic videos and articles.