Get startedGet started for free

Why do we need Programming Paradigms?

1. Why do we need Programming Paradigms?

Now that we have established what programming paradigms are and the two broad categories of paradigms, let's talk about their purpose. In this video, we're going to talk about why we need programming paradigms.

2. Why do we need programming paradigms?

To understand why we need programming paradigms, it is helpful to understand that different problems are best solved with different approaches or ways of thinking. For example, writing a program to solve a number puzzle like Sudoku might look very different from writing a program to calculate a company's annual revenue. That being said, there are certain common goals of any viable programming paradigm, such as producing accurate results in a reasonable amount of time and having code that is relatively easy to understand by other programmers. The different approaches of each programming paradigm to achieve these common goals are what distinguishes them from one another. Operating within a specific programming paradigm means having a standard approach to a common problem, which saves time and effort on the part of the programmer.

3. Benefits of modular code

One such example of a common goal among all programming paradigms is to have code that is modular and easily reusable. Modular code is code that is broken up into sections that might be re-run or reused in different contexts, rather than requiring the same identical code to be written out more than once. The concept of modular code is related to the concept of separation of responsibilities, which means that certain sections of code have different responsibilities from other sections of code, and they are not duplicating logic wherever possible. Both writing modular code and following a pattern of separation of responsibilities in our code are important to pay attention to from the beginning, as failing to do so can cause even bigger problems once the total amount of code becomes large and be hard to fix at that point. Adhering to these principles will reduce the chance of introducing bugs and save development time in the long run.

4. Separation of responsibilities in different paradigms

Each paradigm handles this concept of "separation of responsibilities" a little bit differently. For example, procedural programming, functional programming, and object-oriented programming are all named after the names of the units that they get broken down into: procedures, functions, and objects, respectively. For now, we don't need to worry about the details of these paradigms, but it is useful to understand that much of what defines them as paradigms is the way they handle the concept of separation of responsibilities.

5. Modular code example

Let's take a look at a simple example here of writing modular code in Python. On the left, we have written out the steps to average two values in Python explicitly and then performed this process twice. On the right, we have written a function to perform this process and then called it twice. At this point, it might seem unnecessary, but imagine if we needed to call this function one hundred times. The second approach would be much quicker to write and less prone to errors.

6. Let's practice!

Great! We've covered a lot of material here on the subject of the separation of responsibilities, modular code, and how these relate to why we have different programming paradigms. Let's test this knowledge out in some exercises!