DEV Community

marcellusherlus
marcellusherlus

Posted on • Updated on

Short introduction into common programming paradigms

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.


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.


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

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 about a specific paradigm shift and why it was needed.


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 with code examples.


Examples of imperative languages

  • C++
  • C#
  • Pascal
  • Java
  • Python
  • Swift

Source

Examples of declarative languages

  • SQL
  • Prolog
  • Curry
  • Regular Expressions

Source

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

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)

Procedural

Some say 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


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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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.

Logic

This one would exceed the scope I have set myself for this post, but I would like to refer you to this article.


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.

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.

Feedback is welcome :)

Top comments (0)