An overview of some Paradigms
In this article, we are going to have an overview of some programming paradigms to get a little more familiar with them.
Here we are not going to look at each programming paradigm that exists, because it would be too complex. We are going to have an overview of some popular and common paradigms. Our aim is to define them with simple terms and try to understand them better.
What is a paradigm?
A paradigm is like a set of concepts or thought patterns that includes theories, research methods or postulates. It is a system of assumptions that serves as a model. It is a way to see the reality.
What is the relationship with programming?
In programming, paradigms are a way to classify programming languages based on their features. Each programming language follows one or more paradigms. Most of the major language paradigms were invented between the late 1960s and the late 1970s.
Some paradigms are concerned with implications for the execution model of the language, which specifies “how work takes place”. So, they are concerned with such things as allowing side effects, in other words, when a function modifies the state of something else outside its own scope, or whether the sequence of operations is defined by the execution model. Other paradigms are concerned with the way that code is organized or with the style of syntax and grammar.
Also known as Procedural Programming, Imperative Programming uses statements that change the state of a program. In a way, it is like following a recipe. The Imperative Programming is concerned with defining a linear procedure or sequence of programming statements. It consists of commands for the computer to perform. It could be summarized with the sentence “first do this, next do that”.
The Imperative Programming approach is to treat any solution like a series of steps to be performed. So, it uses procedures, functions, subroutines or methods to split the program in small tasks and that makes possible to reuse the code as many times as we want in the program.
The first Imperative Languages were the machine languages and the hardware implementation of nearly all computers is Imperative because it is designed to execute machine code.
Representative programming languages that use this paradigm would be FORTRAN, COBOL or BASIC.
In a few words, Declarative Programming describes what it does, but not how it does it. So, we program by specifying the result we want, but not how to get it. It means that the control flow is implicit. It means no loops and no assignments.
Representative languages would be HTML, CSS or SQL.
The Structured Programming paradigm removes global variables, GOTO and introduces local variables. It means that we create blocks that contain instructions that don’t depend on any other. Here, the control flow is defined by nested loops, conditionals and subroutines. It also has in main traits things like structograms and indentation.
Representative programming languages that use this paradigm would be C or Pascal.
Functional Programming treats computations as the evaluation of mathematical functions and avoids changing-state and mutable data. So, it sees all subprograms as functions that have arguments and return a single solution based on the input. It means that every time a function is called with the argument x it will produce the same result f(x).
This paradigm allows to pass functions to functions and to return functions from functions. Here, the control flow is expressed by combining function calls, rather than by assigning values to variables.
Representative programming languages would be Haskell, Erlang, Scala, Scheme or Lisp.
The Logic Programming paradigm expresses a set of sentences in a logical form. It means that logical assertions about a specific situation are made, then it is checked if it still true or not. It can be viewed as controlled deduction. We focus on facts stored in memory, called the knowledge base.
It is a form a Declarative Programming and the process flow is reversed because it asserts result first.
One representative language would be Prolog.
In Object-Oriented Programming, or OOP, data is represented as objects. These objects are instances of classes that have attributes, representing data fields, and functions called methods. Objects are separate entities and have their own state which is modified built in methods. In Object-Oriented Programming, programs are based on the sending of messages to objects, that respond by performing operations. This concept also provides data encapsulation and information hiding to protect internal properties of an object.
One representative language would be Smalltalk. However, many programming languages are multi-paradigm and support some elements of OOP and combine them with an Imperative style, like Java, C++, PHP or Python.
Here the control flow is determined by events, such as user actions. It means there are event emitters and listeners. It is dominant in graphical user interfaces. Nevertheless, a lot of software relies on user events for functionality, so we could argue that Event-Driven Programming is present for nearly all kinds of projects.
When we talk about Parallel Programming, we mean that we use parallel hardware to execute computation more quickly. It is mainly concerned with speeding-up computation time. So, multiple actions are executed at the same time.
Parallel Programming could be achieved with C++ and OpenMP, for example.
In Concurrent Programming several computations are executed concurrently. So, it means that only one statement is executed at any point in time, but we have no guarantee which task will be executed in the next step. One task doesn’t have to wait that another one ends before starting.
Concurrent Programming could be achieved with Scala, Go, or Elixir.
Many programming languages combine multiple paradigms to get the advantages of each. This gives us more freedom and flexibility, but it could also increase the complexity of the programs themselves.
C++ and Java fit into that category.
Through this article, we saw what a paradigm is and how it is related to programming. We also had a brief overview of some major programming paradigms by looking at their main concept and how they apply it. We also saw that some programming languages are multi paradigms to give more freedom and to write programs that suit the nature of a specific problem. Many more paradigms exist and some depend on some major paradigms we saw.
One last word
If you like this article, you can consider supporting and helping me on Patreon! It would be awesome! Otherwise, you can find my other posts on Medium and Tumblr. You will also know more about myself on my personal website. Until next time, happy headache!