class: center, middle ### COMP26020 Programming Languages and Paradigms Part 1: C Programming *** # Programming Paradigms ??? - Hi everyone. - I am Pierre, the instructor for part 1 of comp26020 - Before jumping to C and C++ programming in the next videos, in this video you will learn about the concept of programming paradigms and how it relates to programming languages. --- # What's a Programming Paradigm? - A programming paradigm defines a **fundamental style of programming ...** - How the programmer can describe the program in the source code: - the **data** used by the program, and the **computations** manipulating the data ??? - Let's first try to define the term. - To put it simply a programming paradigm is a style of programming. - As the programmer, this style defines how you will describe your program within its source code: the data that is uses, as well as the computations manipulating this data. -- - How do programming paradigms related to programming languages? - **Paradigms can be used to classify languages** based on the programming styles they support ??? - Paradigms are high level categories used to classify programming languages. ---
- **A language belongs to one or several paradigms**, i.e. it makes it easy to develop using the style defined by the paradigm
.small[adapted from https://www.janeve.me/software-programming/understanding-programming-paradigms] ??? - All programming languages fall within at least one paradigm. - What it means is that they make it easy to program in the style defined by the paradigm in question. - For example a lot of operations look pretty similar in C++ and in C sharp because both languages fall within the object oriented programming paradigm. - Also a lot of languages can be classified within several paradigms. - For example objective caml belongs to the object oriented as well as functional programming paradigms. --- # A Paradigm is a Programming Style Example: multiplying by 2 each element of an array in JavaScript ??? - Let's illustrate the fact that a paradigm defines a programming style with an example. - We simply want to multiply by 2 each element of an array. -- 1. Using the **imperative** paradigm: ```js function mult_by_two_imperative (array) { let results = [] for (let i = 0; i < array.length; i++){ results.push(array[i] * 2) } return results } ``` .codelink[
`01-programming-paradigms/mult2-imperative.js`
] ??? - JavaScript allows you to write code according to the imperative programming paradigm, this is the first example. - The imperative paradigm requires to describe step by step every operations performed by the program. - In this example we use a for loop to iterate over all the elements and multiply each by two. - In some sense this is very close to what happens on the CPU, which is executing instructions one step at a time. -- 2. Using the **declarative** paradigm: ```js function mult_by_two_declarative (array) { return array.map((item) => item * 2) } ``` .codelink[
`01-programming-paradigms/mult2-declarative.js`
] Result is the same, the way to describe the computations is different ??? - But JavaScript also allows to write code according to the declarative paradigm, this is the second example. - With the declarative paradigm we describe high level operations to be performed on each element of the array: each item is multiplied by two. - This is more abstract and also more concise than imperative programming. - We can run these two examples using node. - We can see that the result is exactly the same but the way that the programmer described the computations is different. --- # A Programming Paradigm is a Programming Style - Paradigm defines how the programmer describes the program **computations** - Can be sequences of statements, executed sequentially or in parallel - Can be divided into functions - Can also be *how the result should look like* - etc. ??? - How the programmer defines the computations. - It's generally a sequence of statement performing various operations, in various order -- for example sequentially or in parallel. - These computations can be decomposed into units named functions. - The computations can also be described in terms of how the result should look like. -- - Paradigm also defines how the programmer describes the **data** computations are applied on - Basic types - Custom data structures - Local/global variables - etc. ??? - The paradigm also defines how to describe the data that is manipulated by the computations. - It answers questions like what are the basic types, can we define custom data structures by composing these types, can the functions manipulate only a local set of variable or do they have access to a global state. --- # Picking a Programming Paradigm - **Some paradigms are (much) better suited than others to solve a given kind of software engineering problem** ??? - Now here is the important thing with programming paradigms. - Given a particular software engineering problem, let's say you have to build a particular program, there are some programming paradigms that are much more efficient at solving that problem. - For example you cannot really write an operating system with a logical programming language. -- - Choosing a paradigm and a language to solve a given software engineering problem has **huge consequences in terms of the design and behaviour of the solution**, impacting among others: -- - Code structure and modularity, clarity/complexity to understand, amount of LoC -- - Reusability of code (e.g. OOP's inheritance) -- - State (memory, variables/objects, data) management -- - If and how parallelism can be expressed -- - Which errors are handled and how (error codes, exception, undefined behaviour) -- - Compile-/run-time characteristics such as memory footprint and program speed ??? - Now because some paradigms are more efficient than others to solve a given problem, when you need to develop something you picking a language (in other words picking a paradigm) has huge consequences on the efficiency, size, complexity and clarity of your code. --- # Wrapping Up - Programming paradigm → programming style - Each programming language implements one or more paradigms - Choose the right paradigm for the right software engineering problem ---- .center[Feedback form: https://bit.ly/37p8WZ3]
??? - And that's it, let's wrap up. - A programming paradigm defines a programming style. - Languages can be classified within paradigms. - Some languages belong to multiple paradigms. - Some paradigms are better suited than others to solve a given problem.