Introduction To Programming Paradigms
Introduction To Programming Paradigms
It’s the same word we get “emperor” from, and that’s quite apt.
You’re the emperor. You give the computer little orders to do
and it does them one at a time and reports back.
To illustrate, let's find the sum of first ten natural numbers in the
imperative paradigm approach.
Example in C:
#include <stdio.h>
int main()
{
int sum = 0;
sum += 1;
sum += 2;
sum += 3;
sum += 4;
sum += 5;
sum += 6;
sum += 7;
sum += 8;
sum += 9;
sum += 10;
return 0;
}
In the above example, we are commanding the computer what
to do line by line. Finally, we are storing the value and printing
it.
To illustrate, let's find the sum of first ten natural numbers in the
procedural paradigm approach.
Example in C:
#include <stdio.h>
int main()
{
int sum = 0;
int i =0;
for(i=1;i<11;i++){
sum += i;
}
return 0;
}
In the example above, we've used a simple for loop to find the
summation of the first ten natural numbers.
Languages that support the procedural programming paradigm
are:
C
C++
Java
ColdFusion
Pascal
Procedural programming is often the best choice when:
There is a complex operation which includes dependencies
between operations, and when there is a need for clear visibility of the
different application states ('SQL loading', 'SQL loaded', 'Network
online', 'No audio hardware', etc). This is usually appropriate for
application startup and shutdown (Holligan, 2016).
The program is very unique and few elements were shared
(Holligan, 2016).
The program is static and not expected to change much over time
(Holligan, 2016).
None or only a few features are expected to be added to the
project over time (Holligan, 2016).
Why should you consider learning the procedural programming
paradigm?
It's simple.
An easier way to keep track of program flow.
It has the ability to be strongly modular or structured.
Needs less memory: It's efficient and effective.
1.2 Object-oriented programming paradigm
OOP is the most popular programming paradigm because of its
unique advantages like the modularity of the code and the
ability to directly associate real-world business problems in
terms of code.
Describes
the key concepts of object-oriented programming (Source: javatpoint.com)
Objects are instances of classes. Objects have attributes/states
and methods/behaviors. Attributes are data associated with the
object while methods are actions/functions that the object can
perform.
Ex
plains the states and behaviors of an object (Source: guru99.es)
Abstraction separates the interface from implementation.
Encapsulation is the process of hiding the internal
implementation of an object.
Inheritance enables hierarchical relationships to be represented
and refined. Polymorphism allows objects of different types to
receive the same message and respond in different ways.
To illustrate, let's find the sum of first ten natural numbers in the
object-oriented paradigm approach.
Example in Java:
class Addition {
int sum =0;
int num =0;
int addValues(){
for(int i=1; i<=num;i++){
sum += i;
}
return sum;
}
}
We have a class Addition that has two
states, sum and num which are initialized to zero. We also have a
method addValues() which returns the sum of num numbers.
In the Main class, we've created an object, obj of Addition class.
Then, we've initialized the num to 10 and we've
called addValues() method to get the sum.
Languages that support the object-oriented paradigm:
Python
Ruby
Java
C++
Smalltalk
Object-oriented programming is best used when:
You have multiple programmers who don’t need to understand
each component (Holligan, 2016).
There is a lot of code that could be shared and reused (Holligan,
2016).
The project is anticipated to change often and be added to over
time (Holligan, 2016).
Why should you consider learning the object-oriented programming
paradigm?
Reuse of code through Inheritance.
Flexibility through Polymorphism.
High security with the use of data hiding (Encapsulation) and
Abstraction mechanisms.
Improved software development productivity: An object-
oriented programmer can stitch new software objects to make
completely new programs (The Saylor Foundation, n.d.).
Faster development: Reuse enables faster development (The
Saylor Foundation, n.d.).
Lower cost of development: The reuse of software also lowers
the cost of development. Typically, more effort is put into the object-
oriented analysis and design (OOAD), which lowers the overall cost
of development (The Saylor Foundation, n.d.).
Higher-quality software: Faster development of software and
lower cost of development allows more time and resources to be used
in the verification of the software. Object-oriented programming tends
to result in higher-quality software (The Saylor Foundation, n.d.).
1.3 Parallel processing approach
Parallel processing is the processing of program instructions by
dividing them among multiple processors.
man(Socrates).
mortal(X) :- man(X).
The first line can be read, "Socrates is a man.'' It is a base
clause, which represents a simple fact.
The second line can be read, "X is mortal if X is a man;'' in other
words, "All men are mortal.'' This is a clause, or rule, for
determining when its input X is "mortal.'' (The symbol ":-'',
sometimes called a turnstile, is pronounced "if''.) We can test
the program by asking the question:
?- mortal(Socrates).
that is, "Is Socrates mortal?'' (The "?-'' is the computer's prompt
for a question). Prolog will respond "yes''. Another question we
may ask is:
?- mortal(X).
That is, "Who (X) is mortal?'' Prolog will respond " X = Socrates''.
To give you an idea, John is Bill's and Lisa's father. Mary is
Bill's and Lisa's mother. Now, if someone asks a question like
"who is the father of Bill and Lisa?" or "who is the mother of Bill
and Lisa?" we can teach the computer to answer these
questions using logic programming.
Example in Prolog:
father(John, Bill).
The above code defines that John is Bill's father.
We're asking Prolog what value of X makes this statement true?
X should be Mary to make the statement true. It'll respond X =
Mary
?- mother(X, Bill).
X = Mary
Languages that support the logic programming paradigm:
Prolog
Absys
ALF (algebraic logic functional programming language)
Alice
Ciao
Logic programming paradigm is often the best use when:
If you're planning to work on projects like theorem proving,
expert systems, term rewriting, type systems and automated planning.
Why should you consider learning the logic programming paradigm?
Easy to implement the code.
Debugging is easy.
Since it's structured using true/false statements, we can develop
the programs quickly using logic programming.
As it's based on thinking, expression and implementation, it can
be applied in non-computational programs too.
It supports special forms of knowledge such as meta-level or
higher-order knowledge as it can be altered.
2.2 Functional programming paradigm
The functional programming paradigm has been in the limelight
for a while now because of JavaScript, a functional
programming language that has gained more popularity
recently.
The functional programming paradigm has its roots in
mathematics and it is language independent. The key principle
of this paradigm is the execution of a series of mathematical
functions.
Example in JavaScript:
function isPrime(number){
for(let i=2; i<=Math.floor(Math.sqrt(number)); i++){
if(number % i == 0 ){
return false;
}
}
return true;
}
isPrime(15); //returns false
In the above example, we've
used Math.floor() and Math.sqrt() mathematical functions to
solve our problem efficiently. We can solve this problem without
using built-in JavaScript mathematical functions, but to run the
code efficiently it is recommended to use built-in JS functions.
number is scoped to the function isPrime() and it will not be
affected by any values outside its scope. isPrime() function
always produces the same output when given the same input.
NOTE: there are no for and while loops in functional
programming. Instead, functional programming languages rely
on recursion for iteration (Bhadwal, 2019).
Languages that support functional programming paradigm:
Haskell
OCaml
Scala
Clojure
Racket
JavaScript
Functional programming paradigm is often best used when:
Working with mathematical computations.
Working with applications aimed at concurrency or parallelism.
Why should you consider learning the functional programming
paradigm?
Functions can be coded quickly and easily.
General-purpose functions can be reusable which leads to rapid
software development.
Unit testing is easier.
Debugging is easier.
Overall application is less complex since functions are pretty
straightforward.
2.3 Database processing approach
This programming methodology is based on data and its
movement. Program statements are defined by data rather than
hard-coding a series of steps.
Describes how the Persons table will look after the execution
Database processing approach is often best used when:
Working with databases to structure them.
Accessing, modifying, updating data on the database.
Communicating with servers.
Why are databases important and why should you consider learning
database processing approach?
Massive amount of data is handled by the database: Unlike
spreadsheet or other tools, databases are used to store large amount of
data daily.
Accurate: With the help of built-in functionalities in a database,
we can easily validate.
Easy to update data: Data Manipulation Languages (DML) such
as SQL are used to update data in a database easily.
Data integrity: With the help of built-in validity checks, we can
ensure the consistency of data.