0% found this document useful (0 votes)
10 views44 pages

CMP 332-Lect-3

The document provides an overview of programming paradigms and methodologies, emphasizing the importance of managing complexity in programming. It discusses various paradigms such as imperative, procedural, object-oriented, parallel processing, declarative, logic, and functional programming, along with their characteristics and appropriate use cases. Additionally, it highlights the significance of good programming methodology for developing correct, maintainable, and efficient programs.

Uploaded by

etobex17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views44 pages

CMP 332-Lect-3

The document provides an overview of programming paradigms and methodologies, emphasizing the importance of managing complexity in programming. It discusses various paradigms such as imperative, procedural, object-oriented, parallel processing, declarative, logic, and functional programming, along with their characteristics and appropriate use cases. Additionally, it highlights the significance of good programming methodology for developing correct, maintainable, and efficient programs.

Uploaded by

etobex17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

CMP 332

SURVEY OF PROGRAMMING LANGUAGES

SURVEY OF PROGRAMING PARADIGMS AND PROGRAMMING


METHODOLOGY

LECTURE THREE

Delivered By: Dr. T. A. Olowookere


Lesson objectives

By the end of this lesson, students should be able to:

describe paradigm
describe programming methodology
identify good programming methodology
Describe steps of programming
Describe good programming styles
Identify and describe programming errors
PROGRAMMING PARADIGM
“Any fool can write code that a computer can understand. Good programmers write code that
humans can understand.”― Martin Fowler.

When programming, complexity is always the enemy. Programs with great complexity,
with many moving parts and interdependent components, seem initially impressive.
However, the ability to translate a real-world problem into a simple or elegant solution
requires a deeper understanding.
The less complexity we have, the easier it is to debug and understand. The more complex
a program becomes, the harder it is to work on it.

Managing complexity is a programmer’s main concern. So how do programmers deal


with complexity?
There are many general approaches that reduce complexity in a program or make it more
manageable. One of the main approaches is a programming paradigm.
Let's dive into programming paradigms!
WHAT IS PROGRAMMING PARADIGM?
The term programming paradigm refers to a style of programming, a way of thinking
about software construction.
It does not refer to a specific language, but rather it refers to the way you program, a
methodology. Some languages make it easy to write in some paradigms but not in
others.
There are lots of programming languages that are well-known but all of them need to
follow some strategy when they are implemented. And that strategy is a Paradigm.
We use the word Paradigm to mean “any example or model”. This usage of the word
was popularized by the science historian Thomas Kuhn. He used the term to describe a
set of theories, standards and methods that together represent a way of organizing
knowledge- a way of viewing the world.

Thus, a programming Paradigm is a way of conceptualizing what it means to perform


computation and how tasks to be carried out on a computer should be structured and organized.
The types of programming paradigms
The types of programming paradigms
1. Imperative programming paradigm
The word “imperative” comes from the Latin “impero” meaning “I command”.
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.

The paradigm consists of several statements, and after the execution of all of them, the
result is stored. It’s about writing a list of instructions to tell the computer what to do
step by step.

In an imperative programming paradigm, the order of the steps is crucial, because a


given step will have different consequences depending on the current values of variables
when the step is executed.
To illustrate, let's find the sum of first ten natural numbers in the imperative paradigm
approach. An Example in C:
1.1 Procedural programming paradigm
Procedural programming (which is also imperative) allows splitting the instructions
into procedures.
Procedural programming is a programming paradigm, derived from structured
programming, based upon the concept of the procedure call.
NOTE: Procedures aren't functions. The difference between them is that functions
return a value, and procedures do not.
More specifically, functions are designed to have minimal side effects, and always
produce the same output when given the same input. Procedures, on the other hand, do
not have any return value. Their primary purpose is to accomplish a given task and
cause a desired side effect.
A great example of procedures would be the well known for loop. The for loop's main
purpose is to cause side effects and it does not return a value.
The Programming Languages that support the procedural programming paradigm are:
C, C++, Java and Pascal.
To illustrate, let's find the sum of first ten natural numbers in the procedural paradigm
approach.
An Example in C:
1.1 Procedural programming paradigm
1.1 Procedural programming paradigm
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.

The program is very unique and few elements were shared.

The program is static and not expected to change much over time.

None or only a few features are expected to be added to the project over time.
1.1 Procedural programming paradigm
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.
The key characteristics of object-oriented programming include Class, Abstraction,
Encapsulation, Inheritance and Polymorphism.
A Class is a extensible template or blueprint from which objects are created.
Abstraction separates the interface from implementation.
Inheritance enables hierarchical relationships to be represented and refined.
Polymorphism is an object-oriented programming concept that refers to the ability of a
variable, function or object to take on multiple forms. A language that features
polymorphism allows developers to program in the general rather than program in the
specific. Polymorphism allows objects of different types to receive the same message and
respond in different ways.
Encapsulation is an Object Oriented Programming concept that binds together the data
and functions that manipulate the data, and that keeps both safe from outside
interference and misuse. Encapsulation is the process of hiding the internal
implementation of an object.
1.2 Object-Oriented Programming paradigm
The Object-Oriented Programming System
1.2 Object-Oriented Programming paradigm
Example of Inheritance
class Doctor {
void Doctor_Details() {
System.out.println("Doctor Details..."); } }

class Surgeon extends Doctor {


void Surgeon_Details() {
System.out.println("Surgen Detail..."); } }

Example of Polymorphism

void sum (int a , int b);


void sum (int a , int b, int c);
void sum (float a, double b);
1.2 Object-Oriented Programming paradigm
// C++ program for function overloading
# include <bits/stdc++.h>
using namespace std;
class Example
{
public:
// function with 1 int parameter
void func(int x)
{
cout << "value of x is " << x << endl;
}
// function with same name but 1 double parameter
void func(double x)
{
cout << "value of x is " << x << endl;
}

// function with same name and 2 int parameters


void func(int x, int y)
{
cout << "value of x and y is " << x << ", " << y << endl;
}
};
1.2 Object-Oriented Programming paradigm
Object-Oriented Programming (OOP) is a programming paradigm based on the concept
of “objects”, which may contain data, (in the form of fields, often known as attributes);
and code, (in the form of procedures, often known as methods).
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.
1.2 Object-Oriented Programming paradigm
1.2 Object-Oriented Programming paradigm
To illustrate, let's find the sum of first ten natural numbers in the object-oriented
paradigm approach.

The Example in Java:


We have a class Addition that has two states/attributes, 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.

The Programming Languages that support the object-oriented paradigm are:


Python, Ruby, Java, C++, Smalltalk
1.2 Object-Oriented Programming paradigm
Object-oriented programming is best used when:

You have multiple programmers who don’t need to understand each component.
There is a lot of code that could be shared and reused.
The project is anticipated to change often and be added to over time.
1.2 Object-Oriented Programming paradigm
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.
Faster development: Reuse enables faster development.
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.
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.
1.3 Parallel processing approach
Parallel processing is the processing of program instructions by dividing them among
multiple processors.

A parallel processing system allows many processors to run a program in less time by
dividing them up.

The Programming Languages that support the Parallel processing approach are:
NESL (one of the oldest ones)
C
C++
1.3 Parallel processing approach
Parallel processing approach is often the best use when:

You have a system that has more than one CPU or multi-core processors which are
commonly found on computers today.
You need to solve some computational problems that take hours/days to solve even with
the benefit of a more powerful microprocessor.
You work with real-world data that needs more dynamic simulation and modeling.
1.3 Parallel processing approach
Why should you consider learning the parallel processing
approach?

Speeds up performance.
Often used in Artificial Intelligence.
It makes it easy to solve problems since this approach seems to be like a divide and
conquer method.
2. Declarative programming paradigm
Declarative programming is a style of building programs that expresses the logic of a
computation without talking about its control flow.

Declarative programming is a programming paradigm in which the programmer defines


what needs to be accomplished by the program without defining how it needs to be
implemented. In other words, the approach focuses on what needs to be achieved instead
of instructing how to achieve it.

So the main differences are that imperative tells you how to do something and
declarative tells you what to do.
2.1 Logic programming paradigm
The logic programming paradigm takes a declarative approach to problem-solving. It is
based on formal logic.

The logic programming paradigm isn't made up of instructions - rather it's made up of
facts and clauses. It uses everything it knows and tries to come up with the world where
all of those facts and clauses are true.
For instance, Socrates is a man, all men are mortal, and therefore Socrates is mortal.

The Programming Languages that support the logic programming paradigm are:
Prolog
Absys
ALF (algebraic logic functional programming language)
Alice
Ciao
2.1 Logic programming paradigm
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.
Functional programming (FP) is a programming paradigm — a style of building the
structure and elements of computer programs — that treats computation as the evaluation
of mathematical functions and avoids changing-state and mutable data.
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.

You compose your program of short functions. All code is within a function. All variables
are scoped to the function.

In the functional programming paradigm, the functions do not modify any values
outside the scope of that function and the functions themselves are not affected by any
values outside their scope.
2.2 Functional programming paradigm
To illustrate, let's identify whether the given number is prime or not in the
functional programming paradigm.

The Example in JavaScript:


2.2 Functional programming paradigm
To illustrate, let's identify whether the given number is prime or not in the functional
programming paradigm.
The Example in JavaScript:
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).

The Programming Languages Languages that support functional programming


paradigm are:
Haskell, Ocaml, Scala, Clojure, Racket, JavaScript
2.2 Functional programming paradigm
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.

A database is an organized collection of structured information, or data,


typically stored electronically in a computer system. A database is usually
controlled by a database management system (DBMS).
To process the data and querying them, databases use tables. Data can then be
easily accessed, managed, modified, updated, controlled and organized.
A good database processing approach is crucial to any company or organization.
This is because the database stores all the pertinent details about the company
such as employee records, transaction records and salary details.

Most databases use Structured Query Language (SQL) for writing and querying
data.
2.3 Database processing approach
Here’s an example in database processing approach (SQL):
2.3 Database processing approach
The PersonID column is of type int and will hold an integer.
The LastName, FirstName, Address, and City columns are of type varchar and will hold
characters, and the maximum length for these fields is 255 characters.

The empty Persons table will now look like this after execution:
2.3 Database processing approach
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.
PROGRAMMING METHODOLOGY
Programming Methodology teaches the widely-used
programming language along with good software engineering
principles.

Emphasis is on good programming style and the built-in facilities


of the language. It is the effective programming practices that
result in the development of correct programs.

It can also be defined as the process used by an individual or a


team for developing program.
Good programming methodology?
A methodology that enables the lowest-cost and on-schedule
development of programs that are correct, easy to maintain &
enhance.

Correct program?
A program with correct syntax & semantics

Readable program?
A program that is easy to read & understand, and therefore, easy
to maintain & enhance
Design Guidelines
Break your code down into short and simple functions

Do not use global variables


Steps of Programming
In order to develop a good program, the following steps are to be followed:
Step 1: Analysis and understanding of problems
Step 2: Designing of algorithm for the problem
Step 3: Testing the correctness of the algorithm to make sure it solves the problem
efficiently and accurately.
Step 4: Translating the algorithm into a suitable programming language, such as, Java
Step 5: Testing and executing the program. That is, the program developed is
compiled and executed.
Step 6: Documenting the program, which is very essential in programming.
We have two types of documentation: Internal and external
Internal involves putting comments and white spaces in our program source
codes while external are in form of manual for operating and maintaining the
program by any user of the program.
Good Programming styles
1. Choose appropriate names for your variables.
2. Special care must be given to the choice of names for procedures, functions,
3. constants and all variables and types used in different parts of your program.
4. Avoid using attractive names whose meaning has little or nothing to do with the
problem.
5. Avoid choosing names that are too close to each other in spelling or otherwise easy to
confuse, for example, Num1, Num2 etc
6. Be careful in the use of letter i(small letter i), l(small l), O(capital O) and 0(zero)
7. Choose appropriate data types for your variables
8. The program must be well structured for readability
9. Modularize the program for easy maintenance
10. Appropriate documentation formats such as comments and whitespaces must be used in
the program.
11. Test the program with different sets of data (both valid and invalid)
Errors in programming
Errors are defeat or bugs in a program.

Testing: The tasks performed to determine the existence of defects in a program.


Debugging: The tasks performed to detect the exact location of defects in a program.

Types of Errors
Syntax errors
Semantic errors.
Runtime errors
Logic error
Compile time errors
Syntax Errors
They are caused by the code that somehow violates the rules of the language. Easy to
detect and fix errors
Semantic Errors
Occur when a statement executes and has an effect not intended by the programmer, Hard
to detect during normal testing. Often times occur only in unusual & infrequent
circumstances.
Run-Time Errors
Occur when the program is running and tries to do something that is against the rules
Example: Accessing a non-existent variable, property, method, object, etc (e.g. a method
name is misspelled). Sources of these can be determined by a careful reading of the code,
but unfortunately, not always!
Logic error.
Usually made by programmers. The program will definitely compile and run very well but
the result/output gotten will have errors.
Compile time errors.
They are errors brought out during the execution stage of the program development. For
example, when we try to divide a number by zero, it causes memory overflow error.
REFERENCES

https://www.freecodecamp.org/
https://medium.com/@LiliOuakninFelsen/functional-vs-object-oriented-vs-procedural-
programming-a3d4585557f3
S.O. Akinola , “Structured programming with Java”
Akhil Bhadwal. (2019). Functional Programming: Concepts, Advantages, Disadvantages,
and Applications
The Saylor Foundation. (n.d.). Advantages and Disadvantages of Object-Oriented
Programming (OOP)

You might also like