Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

lecture 1

The document provides an overview of various programming paradigms, including imperative, procedural, functional, declarative, and object-oriented programming. It explains the characteristics, advantages, and examples of each paradigm, emphasizing their unique approaches to problem-solving in programming. The document also highlights the importance of understanding these paradigms for effective software development.

Uploaded by

cassandria
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

lecture 1

The document provides an overview of various programming paradigms, including imperative, procedural, functional, declarative, and object-oriented programming. It explains the characteristics, advantages, and examples of each paradigm, emphasizing their unique approaches to problem-solving in programming. The document also highlights the importance of understanding these paradigms for effective software development.

Uploaded by

cassandria
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

Aspects of programming

languages
Lecture 1
Aspects of programming languages

1) Different paradigms of programming languages


2) Introduction to the structures of programming languages
3) Program processing: interpretation, compilation and
macro processing
4) Typing systems: Strong versus weak typing
5) Orthogonality
Different paradigms of programming languages

What is a Programming Paradigm?


Different paradigms of programming languages

Programming paradigms are different ways or styles in which a


given program or programming language can be organized.
Each paradigm consists of certain structures, features, and
opinions about how common programming problems should be
tackled.
Different paradigms of programming languages

The question of why are there many different programming


paradigms is similar to why are there many programming
languages. Certain paradigms are better suited for certain types
of problems, so it makes sense to use different paradigms for
different kinds of projects.
Different paradigms of programming languages

Also, the practices that make up each paradigm have developed


through time. Thanks to the advances both in software and
hardware, different approaches have come up that didn't exist
before.
Different paradigms of programming languages

What a Programming Paradigm is Not


Different paradigms of programming languages

Programming paradigms are not languages or tools.


You can't "build" anything with a paradigm. They're
more like a set of ideals and guidelines that many
people have agreed on, followed, and expanded
upon.
Different paradigms of programming languages

Programming languages aren't always tied to a


specific paradigm. There are languages that have
been built with a certain paradigm in mind and have
features that facilitate that kind of programming more
than others
Different paradigms of programming languages

But there are also "multi-paradigm" languages,


meaning you can adapt your code to fit a certain
paradigm or another (JavaScript and Python are
good examples).
Different paradigms of programming languages

At the same time, programming paradigms aren't


mutually exclusive, in the sense that you could use
practices from different paradigms at the same time
with no problem at all.
Different paradigms of programming languages

Why should we care?


Popular Programming Paradigms

Now that we have introduced what programming


paradigms are and are not, let's go through the most
popular ones, explain their main characteristics, and
compare them.
Imperative Programming

Imperative programming consists of sets of


detailed instructions that are given to the
computer to execute in a given order. It's called
"imperative" because as programmers we dictate
exactly what the computer has to do, in a very
specific way.
Imperative Programming

Say you want to bake a cake. Your imperative


program to do this might look like this:
Imperative Programming

Using an actual code example, let's say we want


to filter an array of numbers to only keep the
elements bigger than 5. Our imperative code
might look like this:
Imperative Programming

Using an actual code example, let's say we want


to filter an array of numbers to only keep the
elements bigger than 5. Our imperative code
might look like this:
Imperative Programming

See that we're telling the program to iterate through


each element in the array, compare the item value
with 5, and if the item is bigger than 5, push it into an
array.
We're being detailed and specific in our instructions,
and that's what imperative programming stands for.
Key Characteristics of Imperative Programming:

Sequence of Commands: The


program consists of a sequence of
commands for the computer to follow.
Key Characteristics of Imperative Programming:

State Changes: The program's state


changes as commands are
executed.
Key Characteristics of Imperative Programming:

Control Structures: Uses control structures


like loops (for, while) and conditionals (if, else)
to control the flow of the program.
Key Characteristics of Imperative Programming:

Variables: Uses variables to store and


manipulate data.
Examples of Imperative Programming Languages:
C
C++
Java
Python (can be used in both imperative and other
paradigms)
Imperative Programming

Imperative programming is widely used because it is


straightforward and closely mirrors the way computers
operate. It allows for fine-grained control over the
program's execution, making it suitable for
performance-critical applications.
Procedural Programming
Procedural programming is a derivation of imperative
programming, adding to it the feature of functions (also known
as "procedures" or "subroutines").
In procedural programming, the user is encouraged to subdivide
the program execution into functions, as a way of improving
modularity and organization.
Procedural Programming

Following our cake


example, procedural
programming may look
like this:
Procedural Programming

You can see that, thanks to the implementation


of functions, we could just read the three
function calls at the end of the file and get a
good idea of what our program does.
Procedural Programming

That simplification and abstraction is one of the


benefits of procedural programming. But within
the functions, we still got same old imperative
code.
Procedural Programming

Procedural programming is a subset of imperative


programming that focuses on the concept of
procedure calls, also known as functions or
subroutines. It encourages breaking down a
program into smaller, manageable pieces, which
can be reused and organized more effectively.
Key Characteristics of Procedural Programming:

Functions/Procedures: The program


is divided into functions or procedures,
each performing a specific task.
Key Characteristics of Procedural Programming:

Modularity: Code is organized into modules


or functions, making it easier to manage,
understand, and debug.
Key Characteristics of Procedural Programming:

Sequence of Commands: Like


imperative programming, it follows a
sequence of commands.
Key Characteristics of Procedural Programming:

State Changes: The program's state


changes as functions are executed.
Key Characteristics of Procedural Programming:

Control Structures: Uses loops (for, while)


and conditionals (if, else) to control the flow of
the program.
Key Characteristics of Procedural Programming:

Variables: Uses variables to store and


manipulate data.
Examples of Procedural Programming Languages

C
Pascal
Fortran
BASIC
Procedural Programming

Procedural programming is widely used because it


promotes code reusability and modularity, making it
easier to develop and maintain complex programs. It
is especially useful for tasks that can be broken
down into smaller, repeatable steps.
Functional Programming
Functional programming takes the concept of functions
a little bit further.
In functional programming, functions are treated as
first-class citizens, meaning that they can be
assigned to variables, passed as arguments, and
returned from other functions.
Functional Programming
Another key concept is the idea of pure functions. A
pure function is one that relies only on its inputs to
generate its result. And given the same input, it will
always produce the same result. Besides, it produces
no side effects (any change outside the function's
environment).
Functional Programming
With these concepts in mind, functional programming
encourages programs written mostly with functions. It
also defends the idea that code modularity and the
absence of side effects makes it easier to identify and
separate responsibilities within the codebase. This
therefore improves the code maintainability.
Functional Programming
Going back to the array
filtering example, we can
see that with the imperative
paradigm we might use an
external variable to store
the function's result, which
can be considered a side
effect.
Functional Programming
To transform this into
functional
programming, we
could do it like this:
Functional Programming
It's almost the same code, but we wrap our iteration
within a function, in which we also store the result
array. In this way, we can assure the function doesn't
modify anything outside its scope. It only creates a
variable to process its own information, and once the
execution is finished, the variable is gone too.
Key Characteristics of Functional Programming

First-Class Functions: Functions are


treated as first-class citizens, meaning they
can be assigned to variables, passed as
arguments, and returned from other
functions.
Key Characteristics of Functional Programming

Pure Functions: Functions that always


produce the same output for the same input
and have no side effects (i.e., they do not alter
any state or data outside their scope).
Key Characteristics of Functional Programming

Immutability: Data is immutable, meaning it


cannot be changed once created. Instead,
new data structures are created from existing
ones.
Key Characteristics of Functional Programming

Higher-Order Functions: Functions


that take other functions as
arguments or return them as results.
Key Characteristics of Functional Programming

Function Composition: Building


complex functions by combining
simpler ones.
Key Characteristics of Functional Programming

Declarative Style: Focuses on what to do


rather than how to do it, describing the
desired result without explicitly listing the
steps to achieve it.
Examples of Functional Programming Languages

Haskell
Erlang
Lisp
Scala
F#
Examples of Functional Programming Languages

Functional programming is particularly useful for tasks that


involve complex data transformations, parallel processing,
and situations where predictability and reliability are crucial. It
promotes code that is easier to reason about, test, and
maintain.
Declarative Programming

Declarative programming is all about hiding away


complexity and bringing programming languages closer
to human language and thinking. It's the direct opposite
of imperative programming in the sense that the
programmer doesn't give instructions about how the
computer should execute the task, but rather on what
result is needed.
Declarative Programming

This will be much clearer with an example. Following


the same array filtering story, a declarative approach
might be:
Declarative Programming

See that with the filter function, we're not explicitly


telling the computer to iterate over the array or store
the values in a separate array. We just say what we
want ("filter") and the condition to be met ("num > 5").
Declarative Programming

What's nice about this is that it's easier to read


and comprehend, and often shorter to write.
JavaScript's filter, map, reduce and sort
functions are good examples of declarative code.
Declarative Programming

Another good example are modern JS


frameworks/libraries like React. Take this code for
example:
Declarative Programming

Here we have a button element, with an event


listener that fires a console.log function when the
button is clicked.
Declarative Programming

JSX syntax (what React uses) mixes HTML and JS in


the same thing, which makes it easier and faster to
write apps. But that's not what browsers read and
execute. React code is later on transpiled into regular
HTML and JS, and that's what browsers run in reality.
Declarative Programming

JSX is declarative, in the sense that its purpose is to


give developers a friendlier and more efficient
interface to work with.
An important thing to notice about declarative
programming is that under the hood, the computer
processes this information as imperative code
Declarative Programming

Following the array example, the computer still iterates over the
array like in a for loop, but as programmers we don't need to
code that directly. What declarative programming does is to hide
away that complexity from the direct view of the programmer.
Key Characteristics of Declarative Programming

High-Level Abstraction: Focuses


on the desired outcome rather than
the steps to achieve it.
Key Characteristics of Declarative Programming

Immutability: Data is often


immutable, meaning it cannot be
changed once created.
Key Characteristics of Declarative Programming

No Side Effects: Functions and expressions


do not produce side effects, making the code
more predictable and easier to debug.
Key Characteristics of Declarative Programming

Declarative Syntax: Uses a


declarative syntax to describe the
desired result.
Examples of Declarative Programming Languages

SQL: Used for querying and manipulating databases.


HTML: Used for defining the structure of web pages.
CSS: Used for styling web pages.
Prolog: Used for logic programming.
Declarative Programming

Here's a nice comparison between imperative


and declarative programming.
Comparison with Imperative Programming

Imperative Programming: Specifies the exact steps to


achieve a result, focusing on the control flow.
Declarative Programming: Specifies the desired result,
focusing on the logic and outcome.
Declarative programming
Declarative programming is particularly useful for tasks
that involve complex data transformations, querying
databases, and defining user interfaces. It promotes
code that is easier to read, maintain, and reason about.
Object-Oriented Programming

One of the most popular programming paradigms is


object-oriented programming (OOP).
The core concept of OOP is to separate concerns into entities
which are coded as objects. Each entity will group a given set
of information (properties) and actions (methods) that can be
performed by the entity.
Object-Oriented Programming

OOP makes heavy usage of classes (which are a way of


creating new objects starting out from a blueprint or
boilerplate that the programmer sets). Objects that are
created from a class are called instances.
Object-Oriented Programming

Following our pseudo-code cooking example, now let's say in


our bakery we have a main cook (called Frank) and an
assistant cook (called Anthony) and each of them will have
certain responsibilities in the baking process. If we used
OOP, our program might look like this.
Object-Oriented Programming
Object-Oriented Programming

Object-Oriented Programming (OOP) is a popular


programming paradigm that organizes software design
around data, or objects, rather than functions and logic. An
object can be defined as a data field that has unique
attributes and behavior.
Key Concepts of OOP

Classes and Objects:


● Class: A blueprint for creating objects. It defines a
datatype by bundling data and methods that work on
the data into one single unit.
● Object: An instance of a class. It is created using the
class blueprint and can have its own unique data.
Key Concepts of OOP

Encapsulation:
● The bundling of data (attributes) and methods
(functions) that operate on the data into a single unit or
class. It restricts direct access to some of an object's
components, which can prevent the accidental
modification of data.
Key Concepts of OOP

Inheritance:
● A mechanism where a new class inherits
properties and behavior (methods) from an
existing class. This promotes code reusability
and establishes a natural hierarchy.
Key Concepts of OOP

Polymorphism:
● The ability to present the same interface for
different underlying data types. It allows methods
to do different things based on the object it is
acting upon, even if they share the same name.
Key Concepts of OOP

Abstraction:
● The concept of hiding the complex implementation details
and showing only the necessary features of an object. It
helps in reducing programming complexity and effort.
Examples of OOP Languages

Java
C++
Python
Ruby
C#
Examples of OOP Languages

OOP is widely used because it helps in organizing


complex programs, making them easier to manage,
understand, and extend. It also promotes code
reusability and scalability.
Activity

You might also like