Short introduction into common programming paradigms
Tags: beginners, writing, blogging, coding
Published: 2020-05-22 13:53:56

_This article was originally posted at:
https://kindred.one/2020/05/22/Programming-Paradigms_

This is my first post on a platform ever and it has several goals for myself.

- Firstly I’m not a fluent english speaker (I’m from Germany ) so I want to train my english with posts like this and I will hopefully find the motivation to write more in the future
- Secondly I have seen that I’m quite unfamiliar with programming paradigms and working on this article has forced me to dig deeper into that topic
- Last but not least I hope that I can help some of you guys to get a basic understanding of different paradigms

So let’s start our journey.
<br/><br/>

## The Term Paradigm

Often used in scientific fields or the business world, it defines a thinking approach and different procedures to solve specific problems.

Let me explain it using an example:

A common paradigm is to lecture the students, drown them in theories, books and paperwork.

A **paradigm shift** would be, if you now get a new cool teacher who lets your class work and solve problems in groups and through cooperating.

**If you change paradigms you change the way you think about a specific problem.**
<br/><br/>

## Programming Paradigms

Are a way to classify different programming languages. There are different ways to do so.

- Evaluate the syntax and grammar of a language
- Evaluate the logical structure of a program (how the code is organised)
- And [some more](https://en.wikipedia.org/wiki/Programming_paradigm)
<br/><br/>

## Why do we have/need different paradigms?

As I mentioned before, a paradigm is just a way of looking and thinking about a problem. There will always be the possibility to solve a problem with another paradigm.

Additionally it might be, that one paradigm may be better, more efficient or just easier for solving a given problem.

I found a very good [blog post](https://blog.redelastic.com/what-is-reactive-programming-bc9fa7f4a7fc)  about a specific paradigm shift and why it was needed.
<br/><br/>

## Basic Paradigms

There is a plethora (a new word i’ve learned yay! <(^ . ^)>) of programming paradigms out there and I will cover some of the basic ones, which are luckily just a few.

In General there are two main paradigms, while each following paradigm is a subset of one of them.

- **Imperative** (Give a step by step instruction on what to do ➝ HOW to solve a specific problem)
- **Declarative** (Give the task to be solved ➝ WHAT we want to be solved)

Here is a great [answer](https://stackoverflow.com/a/1784702/13301308) with code examples.
<br/><br/>

### Examples of imperative languages

- C++
- C#
- Pascal
- Java
- Python
- Swift

[Source](https://en.wikipedia.org/wiki/List_of_programming_languages_by_type#Imperative_languages)

### Examples of declarative languages

- SQL
- Prolog
- Curry
- Regular Expressions

[Source](https://en.wikipedia.org/wiki/List_of_programming_languages_by_type#Declarative_languages)

## Other Paradigms

As mentioned before, you can assign other ‘sub-paradigms’ to one of the two main paradigms (imperative, declarative).

- Imperative
    - Structured
    - Procedural
    - Object oriented
- Declarative
    - Functional
    - Logic
<br/><br/>


### Structured

Add simple program flow structures to the coding syntax. The most popular and frequent structures that come with this paradigm are **iteration**, **selection** and **subroutines**

- **Iteration** A control structure that allows some lines of code to be executed many times (while/ for loops)
- **Selection** A control structure where the program chooses between two or more options. (if blocks)
- **Subroutines** A control structure that defines callable units (methods, functions, classes)
<br/><br/>

### Procedural

[Some say](https://en.wikipedia.org/wiki/Procedural_programming) the **Procedural** paradigm derives from the **Structured** paradigm.

Since the borders between this two paradigms are unclear I would rather not cover **Procedural** to not mess things up.

But if you are interested in that, take a look [here](https://softwareengineering.stackexchange.com/questions/117092/whats-the-difference-between-imperative-procedural-and-structured-programming)
<br/><br/>

### Object Oriented

Object-oriented programming (OOP) is by far the most known programming paradigm.

It based on the concept of “objects”, which may contain data in the form of fields (attributes) and methods.

For example, a cat is an object which has certain attributes such as size, race, age, etc. It also has certain methods such as jump, eat, run and so on.

```
class Cat

  float size
  char race
  int age

  function jump(height: int)
    # Code for jumping

  function run(speed: int)
    # Code for running
```

To interact with a cat you just need to create one and call it’s functions

```
class CatSimulator2000

  Cat leo = new Cat()
  leo.jump(20)
  leo.run(10)
```
<br/><br/>

### Functional

Although the functional paradigm is a prehistoric rock it got quite more attention with the rise of multi-core processing. Through them the interest in functional programs that support concurrency more readily has increased.

**Principles**

One of the main concepts in functional programming is the elimination of side effects and hidden impacts in a code base. To achieve that there are some rules to follow.

1. Immutable State & Variables
2. Higher Order Functions
3. Recursion instead of Loops
4. Pure Functions

Since this topic is too big to cover in this brief overview, I recommend you to read this [post](https://www.keycdn.com/blog/functional-programming).

### Logic

This one would exceed the scope I have set myself for this post, but I would like to refer you to this [article](https://hackernoon.com/try-logic-programming-with-c-compiler-602a8977406f).
<br/><br/>

## Conclusion

There are a lot of different paradigms out there,  but only a few are used frequently. In software projects you will mostly see OOP and functional programming languages in production. If you encounter other ones, they will be at least some sub-form of imperative or declarative.

It’s worth mentioning that a lot of programming languages can be assigned to one ore more paradigm. We call that [multi-paradigm languages](https://en.wikipedia.org/wiki/Comparison_of_multi-paradigm_programming_languages#Language_overview).

**I hope I was able to provide you with a basic overview about some of the classic programming paradigms. If you’re brave and want to learn everything about the paradigms in the world then one of the following links could be a good starting point.**

- https://en.wikipedia.org/wiki/List_of_programming_languages_by_type
- https://en.wikipedia.org/wiki/Programming_paradigm

*Feedback is welcome :)*