2. Inside
What is Programming Paradigms?
Imperative vs Declarative Programming Paradigms
4 Major Programming Paradigms
Procedural Programming Paradigm
Logical Programming Paradigm
Functional Programming Paradigm
Object Oriented Programming Paradigm
3. Programming Paradigm
Is an approach to programming a computer;
Based on mathematical theory or
A coherent set of principles
4. Imperative – Declarative Programming
Paradigms
Question :
I’m right next to Eagle Statue. How do I get to Digiturk from here?
How can we define it with Imperative PP?
How can we define it with Declarative PP?
5. Imperative Way
Head east on Mumcu Bakkal Sokağı toward Şht. Asım Cd.
Turn left onto Şht. Asım Cd.
Continue onto Masuklar Ykş. (110m)
Continue onto Yıldız Cd. (270m)
Digiturk is on the right
7. Imperative programming is like how you do something,
while declarative programming is more like what you do.
8. Another Example
You go to a restaurant and approach the front desk
If you say :
I see that table located under the TV is empty
I would like to sit there
IMPERATIVE (define how to get a table)
Or if you say :
Table for one, please
DECLARATIVE (what you want)
9. Many (if not all) declarative approaches
have some sort of underlying imperative abstraction.
10. Imperative-Declarative Relation
The declarative response for restaurant assumes that the front-desk guy knows..
Knowing the Digiturk address assumes you have a some sort of GPS that knows..
‘THE IMPERATIVE STEPS’
11. Examples of PP with
Programming Languages
SQL :
HTML :
<article>
<header>
<h1>Declarative Programming</h1>
<p>Sprinkle Declarative in your verbiage to sound smart</p>
</header>
</article>
SELECT * FROM Users WHERE Country=’Mexico’;
12. Examples of PP with
Programming Languages
Javascript (write a function called double which takes an array, and return a new array where
the values doubled) :
function double (arr) {
let results = []
for (let i = 0; i < arr.length; i++){
results.push(arr[i] * 2)
}
return results
}
function double (arr) {
return arr.map((item) => item * 2)
}
13. Programming Languages by Paradigms
Imperative: C, C++, Java
Declarative: SQL, HTML
(Can Be) Mix: JavaScript, C#, Python
16. Procedural Programming Paradigm
The entire program is a single algorithm consists of independent modules
The complete functionality written linearly (step-by-step)
18. Procedural PP Advantages
Efficient (modules are easier to write)
Close to the machine
Best for expressing algorithms
19. Procedural PP Disadvantages
The semantics of the program can be complex
Hard to understand and hard to prove (lots of modules)
Side effects make debugging harder
Order is crucial (step-by-step)
20. Object Oriented Programming Paradigm
Real-world objects are each viewed as seperate entities
Each object has it’s own state which is modified only by built in methods
Objects operated independently (communication is done by message passing)
22. OOP PP Advantages
Inheritance (reusable code and code extensibility)
Changing code without the need to change existing code (subclass)
Encapsulation and information hiding
Prevents data from being accessed by the code outside
Define data as private and open them to outside by public methods
Parallel code development with big teams
More human-friendly and readable code
23. OOP PP Disadvantages
First time coding can be more extensive (code duplication)
Can create a massive amount of unnecessary code
Can be slow because of the overhead (negligible)
25. Logical Programming Paradigm
Execution of a logic program is a theorem proving process (Datalog, Prolog)
Program statements express facts, rules and predicates
It consists from a system of formal logic
26. Logical PP Example
Given information about fatherhood and motherhood, determine grand parent relationship :
Given the information called facts;
John is father of Lily – father(john, lily)
Kathy is mother of Lily – mother(kathy, lily)
Lily is mother of Bill – mother(lily, bill)
Ken is father of Karen – father(ken, karen)
Who are grand parents of Bill?
grandparent(X,Z) :- parent(X,Y), parent(Y,Z)
parent(X,Y) :-father(X,Y)
parent(X,Y) :-mother(X,Y).
27. Logical PP Advantages
The system solves the problem
Programming steps are kept to a minimum
Proving the validity of the program is simple
28. Logical PP Disadvantages
Not efficient as other programming paradigms
Takes too much effort to create a program
Not widely used (medical diagnosis, robot control etc.)
29. Functional Programming Paradigm
Views all subprograms as functions (in mathematical sense)
Functions take in arguments and return a single solution
Solution returned is based entirely on the input
The time which a function is called has no revelance
31. Functional PP Advantages
High level of abstraction
Functions suppress many of the programming details
Removes the possibility of making too many errors (simple)
Good candidates for programming to parallel computers
32. Functional PP Disadvantages
Problems involving many variables or
Problems with a lot of sequential code runs;
Sometimes easier to implement with Object oriented programming