Programming Paradigms? What? What does that even mean? Well I’m about to tell you. I briefly learnt in my Software Engineering class recently what is a programming paradigm so I decided to share with you what I learnt and researched in a way that you will understand it if you haven’t quite wrapped your head around it.
The term programming paradigm is the style or way of thinking about and approaching problems. Simply, it is a style of programming. There can be more than one type programming paradigms. Here’s a list of the most common ones:
Let’s briefly describe each one.
Imperative programming is the style of programming in which there is a sequence of statements that change the state of the program. First you must understand what is state. The example of state I use involves a switch. The switch can have two states. It can either be on, or it can either be off. In a program, state are values that have an identity. Here’s an example:
var total = 0;
var a = 1;
var b = 5;
total = a + b
In the above example, the program adds to variables together and assigns them to the total variable. The state of the total variable changed from 0 in the beginning of the program, to 6 before the print function.
Imperative programming says how to do something. An example my teacher used was the process of baking a cake. The program says how to do something in the correct sequence it should be done, therefore order of execution (the order in which each statement is executed) is important. Obviously you cannot add candles if you didn’t bake the cake right?
Some examples of imperative programming languages are: Algol and Pascal.
The Functional programming paradigm is mainly used to perform mathematical functions without changing the state. This paradigm can contain concepts like: pure functions, higher order functions and recursion(functions that call itself). It is best used for performing operations on data that is fixed. Basically where there is few things and more operations to be done.
The functional programming paradigm says what is to be done rather how to do something as with imperative. As with the same cake example, the picture below shows the process of baking a cake “functionally.” It says what is to be done, but not how to do it.
Some examples of functional programming languages are: Scala and Haskell.
In object-oriented, everything is represented as an object. Think of a chair. A chair is an object. All chairs have nearly the same properties but different values associated to the properties. Example, one chair can have 4 separate legs whereas another chair can have just two as with a rocking chair. Simply put, objects can have properties with values or methods associated with the properties.
In the image below, the book is the main object. It has properties and methods. From that object, two children objects (E-Book & Paperback) are created. They inherit the properties of the main book object as well as its methods.
Wikipedia has some nice information to learn more. Check it!
It follows the same characteristics as imperative with a little extra features such as various object oriented programming concepts like: Abstraction, Inheritance, Encapsulation and Polymorphic.
This brings me to end of my brief summary on programming paradigms. I hope you learnt something and thank you very much for reading!