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 we move on to C and C++ programming in the next videos, in this video you will learn about the concept of programming paradigms and how they relate to programming languages. --- # What's a Programming Paradigm? - A programming paradigm is a high-level category of programming languages -- - A paradigm defines a **fundamental style of programming** - How the programmer can describe in a program's source code: 1. The **data** used by the program, 2. 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 it uses, as well as the computations manipulating this data. -- - **Paradigms are used to characterise and classify languages**, based on the programming styles they support ??? - Paradigms are high-level categories used to classify programming languages. --- # Languages & Paradigms
- **A language belongs to one or several paradigms**, i.e. it makes it easy to develop using the style defined by the paradigm
.verysmall[Diagram 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, OCaml belongs to both the object-oriented and the functional programming paradigms. --- # A Paradigm is a Programming Style Example: multiplying each element of an array by 2 in JavaScript ??? - Let's illustrate the fact that a paradigm defines a programming style with an example. - We simply want to multiply each element of an array by 2. -- 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_norun[
`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 describing step by step every operation 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 executes 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_norun[
`01-programming-paradigms/mult2-declarative.js`
] Result is the same; the way the computations are described is different ??? - But JavaScript also allows writing 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.js. - We can see that the result is exactly the same, but the way that the programmer described the computations is different. --- # A Paradigm is a Programming Style (2) - The paradigm defines how the programmer describes the program's **computations** - Can be sequences of statements, executed sequentially or in parallel - Can be divided into functions or not - Can also be *what the result should look like* - Can be more or less restrictive on what operations can be used - etc. ??? - It defines how the programmer describes the computations. - It's generally a sequence of statements performing various operations, in various orders -- for example sequentially or in parallel. - These computations can be decomposed into units named functions. - The computations can also be described in terms of what the result should look like. -- - The paradigm also defines how the programmer describes the **data** that computations are applied to - Basic types, custom data structures, local/global variables, access permissions, sizes, etc. ??? - The paradigm also defines how to describe the data that is manipulated by the computations. - It answers questions such as: what are the basic types, can we define custom data structures by composing these types, and can the functions manipulate only a local set of variables or do they have access to a global state? --- # Picking a Programming Paradigm - **Given a particular software engineering problem, some paradigms are much better suited than others to address that 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, some programming paradigms are much more efficient than others at solving that problem. - For example, you cannot really write an operating system with a logic programming language. -- - Picking a language, and as such a paradigm, has **huge consequences on the design, implementation, and behaviour of the solution**, impacting, among other things: -- - Code structure and modularity, clarity/complexity to understand, number of LoC -- - Reusability of code (inheritance, polymorphism, etc.) -- - State management (memory, variables/objects, data) -- - If and how parallelism can be expressed -- - Which errors are handled and how (error codes, exceptions, undefined behaviour) -- - Compile-/run-time characteristics such as memory footprint and program speed ??? - Now, because some paradigms are more efficient than others at solving a given problem, when you need to develop something, 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 - **TODO this week: check the schedule!** video + quiz + set up both formative and summative development environments ---- .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.