Programming paradigms reloaded


by - posted

programming paradigmsWhat is a programming paradigm ?

A paradigm is basically a pattern or model. A programming paradigm is a fundamental style of computer programming. This style shows the way of building the structure and elements of programs.
In this article, I discuss some programming paradigms like : Imperative, Procedural, Structured, Object oriented, Event driven, Concurrent, Declarative, Functional and Logic.

The Programming paradigms reloaded article is an extension and update of my article from January 2014 (Programming paradigms).

1) Imperative

Keywords : algorithms, data

The imperative paradigm follows the model of computation described in the Turing Machine.

Imperative programming is a programming paradigm that uses statements that change a program’s state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform. Imperative programming focuses on describing how to find a solution.

Imperative language examples : Assembler, C, FORTRAN, Algol, Cobol …

Code example (people name sort)

result = []
i = 0
start:
numPeople = length(people)
if i >= numPeople goto end
p = people[i]
nameLength = length(p.name)
if nameLength <= 5 goto next
upperName = toUpper(p.name)
addToList(result, upperName)
next:
i = i + 1
goto start
end:
return sort(result)

2) Procedural

Keywords : algorithms, data

Procedural programming is a type of imperative programming.

The procedural programming paradigm designs programs containing the code of the main program and one or more procedures.
Procedures are also named as : functions, routines, subroutines. The procedures can be invoked anywhere from the main program and by other procedures as well.
These procedures typically take some input, do something, then produce some output. Ideally, procedures would behave as black boxes where input data goes in and output data comes out.
The key idea here is that procedures have no intrinsic relationship with the data they operate on.

Procedural language examples : C, FORTRAN, Algol, Cobol …

Code example (factorials)

int fact(int n) {
int f = 1;
while (n > 1) {
f = f * n;
n = n – 1;
}
return f;
}

3) Structured

Keywords : algorithms, data

Structured programming is a type of imperative programming.

Structured programming is a programming paradigm aimed at improving the clarity, quality and development time of a program by making extensive use of procedures, block structures as well as for- and while-loops. The structured paradigm avoids to use simple tests and jumps such as the goto statement which could lead to “spaghetti code”. This non structured code is difficult to follow and to maintain.

Structured language examples : Pascal, Modula, Oberon, ADA  …

Code example (people name sort)

result = [];
for i = 0; i < length(people); i++ {
p = people[i];
if length(p.name)) > 5 {
addToList(result, toUpper(p.name));
}
}
return sort(result);

4) Object oriented

Keywords : objects + messages

Object oriented programming is basically a type of structured programming.

In object oriented programming, we can imagine a program as a collection of interacting objects. Objects have state, behavior and identity. Data and code are encapsulated in objects. The objects can send messages to each other (message passing). Everything an object can do is represented by its message interface. So you don’t have to know anything about what is in the object in order to use it.
A class is a blueprint (template) of an object,

The OO paradigm is characterized by encapsulation, inheritance, polymorphism and message passing.

  • Encapsulation
    Encapsulation is combining code and data in a single unit (object). Encapsulation is about hiding complexity. You don’t need to know the internals in order to use an object. Using an interface to interact with an object is an example of hiding complexity.
  • Inheritance
    Inheritance is a mechanism that allows the definition of one abstract data type (object) by deriving it from an existing abstract data type (class) . The newly defined type (object) inherits the properties of the parent type (class).
  • Polymorphism
    Sub classes of classes may have functions with the same name, but they will have different behaviors.
    For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of sub classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to a sub class will return the specific result.
  • Message passing
    Message passing is a common mechanism for passing data between objects. Message passing is the process in which an object sends data to another object, either inside the same program or to an object in another program.

Object oriented language examples : Simula, Smalltalk, C++, etc.

Code example (people name sort)

result = []
for p in people {
if p.name.length > 5 {
result.add(p.name.toUpper);
}
}
return result.sort;

5) Event driven

Keywords : main loop, events

Event driven programming is basically a type of procedural programming.

Event driven programming means generally a main loop that listens for events and then triggers a function when one of those events is detected. The program flow is determined by asynchronous events. Events can be user triggered (like a mouse click), hardware interrupts (like sensor signals) or software interrupts. GUI programming uses event driven programming as a basic element.

Event-driven language examples : Visual-Basic …

6) Concurrent

Keywords : switch between processes

Concurrent programming is basically a type of procedural programming.

Concurrent programming involves running multiple processes in an interleaving manner. A process can advance without waiting for all other processes to complete.

The two fundamental concepts in concurrent programming are processes and resources. A process corresponds to a sequential computation. Processes may share resources. Shared resources include :
– program resources like data structures
– hardware resources like : CPU, memory and input/output devices

There must be some “control” structures for concurrent programming :
– the scheduler for executing the processes (synchronous/asynchronous)
– the controller for interaction (communication) between processes
– the dispatcher for allocating the resources

Concurrent language examples : Scala, Erlang, Limbo …

7) Declarative

Keywords : result

Declarative programming focuses on what the program should accomplish without specifying how the program should achieve the result. Declarative programming describes the desired results without explicitly listing commands or steps that must be performed. The program is structured as a collection of properties to find the expected result, not as program-steps to follow.

The declarative paradigm is an umbrella term that includes other (sub-) paradigms like the functional and the logic paradigm (see below).

Declarative language examples : SQL

Code example (factorials)

int fact(int n) { return n > 1 ? n * fact(n – 1) : 1; }

Code example (people name sort)

select upper(name)
from people
where length(name) > 5
order by name

8) Functional

Keywords : mathematical functions

Functional programming is a subset of declarative programming.

Functional programming is based on a formalism called the lambda calculus.

Programs written using this paradigm use code of mathematical functions. Functional programming emphasizes the evaluation of expressions, rather than execution of commands.

FP allows the programmer to think about the problem at a higher level of abstraction, It encourages thinking about the nature of the problem rather than about the sequential nature of the underlying computing engine.

A functional programming language usually has three main sets of components:

data structures           such as a list or arrays
built-in functions        for manipulating the basic data structures
functional forms         also called high-order functions, for building new functions

Functional language examples : Scheme, Haskell, Miranda…

Code example (factorials)

fun fact n = if n < 1 then 1 else n * fact(n-1)

Code example (people name sort)

let(
f, fun(
people,
if(equals(people, emptylist),
emptylist,
if(greater(length(name(head(people))), 5),
append(to_upper(name(head(people))), f(tail(people))),
f(tail(people))))),
sort(f(people)))

9) Logic

Keywords : axioms + logic rules

Logic programming is a subset of declarative programming.

The logic paradigm is based on a formalism called Horn Clauses.

Programs in this paradigm do not describe how to reach the solution of a problem, they explain what the solution is.

This programming paradigm is based on formal logic. A logic program is a collection of logical declarations describing the problem to be solved. The programmer is responsible for specifying the basic logical relationships and does not specify the manner in which the inference rules are applied.

A logic program consists of:

axioms  :  defining facts about objects
rules  :  defining ways for inferencing new facts
a goal statement  :  defining a theorem, potentially provable by given axioms and rules

The Logic Paradigm works well in domains that deal with the extraction of knowledge from basic facts and relations.

Logic language examples : Prolog …

Code example (factorials)

fact(N, 1) :- N < 2.
fact(N, F) :- N >= 2, NX is N – 1, fact(NX, FX), F is N * FX.

If you enjoyed this article, you can :
– get post updates by connecting to our RSS feed
– get post updates by subscribing to our e-mail list
– share on social media :

Leave a comment