Introduction to Programming Paradigms

by Gautham Pai

Introduction to Programming Paradigms

Let's start with this question - how long have you known your mother tongue? From how many years have you been speaking in English?

We perhaps know our mother tongue for quite some time now. We perhaps even know English from a long time (assuming English is not our mother tongue). But can we say that we are authors or poets in these languages?

For most of us, the answer is "No"! We may know a language for a long time, but that doesn't automatically make us a "poet" in the language. What makes a poet different?

A poet knows to twist the language to meet their needs. They know how to replace one word with another, or replace one word with two, or two with one. They know how to rearrange the words and still mean the same thing. They know how to extract maximum value out of the language.

Similar is the case with programming languages. We can learn the basic constructs of a programming language very quickly. Once we have learnt a few programming languages, the next language is not very hard to learn. We can learn the basic constructs in a matter of a few hours.

However programming language "mastery" is altogether a different thing. This can take years! So how do we gain such mastery? How do we become a "poet" in the language?

This is what forms the second dimension of learning programming languages - programming paradigms.

What are programming paradigms?

Paradigms are simply ways to approach problem solving in a certain language.

Sometimes, people who are new to programming ask this question - how many programming languages should we learn? Isn't it enough if we just learn one?

What happens when we know only a few languages? We will only know the paradigms that the language has and we wouldn't have come across other paradigms. We perhaps don't even know what other paradigms exist and we are missing out on our experience of know the different approaches possible to solving problems.

The best way we can explain this is with movie genres.

Movies have various genres - comedy, art, science fiction, drama, horror, action etc. Some movies are known for certain genres. For example, we will perhaps categorize, "The Matrix" as a "Science Fiction" movie. Does it mean a movie has only one genre? No. A movie can have many genres. For example, "The Matrix" can also be categorized as an "Action" movie.

Some genres are stronger than the other when it comes to specific movies. For example, we will perhaps say that "Johny English" is a "Comedy" movie, while "The Matrix" is a "Science Fiction" movie.

While a movie can have many genres, it certainly does not have all genres. For example, "The Matrix" and "Johny English" are certainly not "Horror" movies.

Now imagine our friend, David, has never watched horror movies in his life. Don't you think David is missing something in his experience of watching movies? Imagine if David says he doesn't like horror movies. We tell him, "Watch at least one, because you don't even know what you are missing!"

Now if David asks us to suggest a movie for a specific genre we pick the movie known for the specific genre, not just any movie that has some flavours of that genre. For example, if David asks us to suggest a comedy movie, we will not suggest "The Matrix" just because it has some comedy somewhere. We will pick some of the best comedy movies we know!

If we want to say we have watched a wide variety of movies, we should have watched at least a few movies in each genre. Similar is the case with our exploration of programming languages.

A single programming language can have many paradigms, just like a single movie can have many genres. Now, just like how a movie is known for a certain genre, a language has one strong paradigm. It is usually known for that paradigm. For example, we say, "Java" is an "Object Oriented Programming" language. It doesn't mean Java is only Object Oriented. Java has many other paradigms like Imperative, Aspect Oriented, Functional etc.

Paradigms are not like "either-or". Using a certain paradigm in our code will not prevent us from using others. For example, a piece of code can have flavours of imperative, object oriented, functional and aspect oriented programming at the same time. So usually we mix multiple paradigms together.

Now, certain problems can be solved with more than one paradigm, but some paradigms are better suited for specific types of problems when compared to some others. So when faced with a problem we analyze the best way to solve it in the specific language that we are using and we choose the best paradigm.

Similar to movie genres, a programming language does not have all paradigms. A certain programming language is better known for a certain genre, when compared to others. So when learning a programming paradigm, pick the language that is known for that paradigm. For example, if someone asks to recommend a language to learn "Functional programming", we will suggest, "Python" or, even better, "Haskell", because these languages have a strong functional paradigm built into them. We will not suggest "Java" just because "Java" has some functional aspects built into it. We will recommend "Java" when someone asks for a suggestion to learn "Object Oriented Programming".

This should also answer the question, "Is it enough to learn one programming language?" That's like asking, is it enough to watch one movie. We know that our experience of movie watching is not complete unless we have seen enough in a wide variety of genres. Similarly, if we haven't seen enough programming paradigms, we don't even know what we are missing.

It takes a certain time to grasp a new paradigm. We don't get it initially. It takes a certain effort to wrap our head around it. But then there is an "a-ha" moment and suddenly "we get it" and then we see the world differently.

The good thing about paradigms is, once we have learnt a certain paradigm with one programming language we can easily relate to the same paradigm in another language. As an example, if we have learnt functional programming in Python, it is not very hard to learn the same in JavaScript. There will of course be subtle differences, but we are usually able to grasp it much faster.

Here are some popular paradigms used in the industry today:

  • Procedural Programming
  • Functional Programming
  • Object Oriented Programming
  • Aspect Oriented Programming
  • Declarative Programming
  • Asynchronous Programming

Learn more about paradigms here:

Test Your Knowledge

No quiz available

Tags