Programming Paradigms: Imperative
Hello there,
In the previous blog post, we covered the imperative and declarative paradigms in detail. In this blog post, we are going to cover the paradigms, which are influenced by the imperative paradigm. If you haven’t read my previous blog on paradigms, then I would recommend you to read it before going further.
Following are the few paradigms which are influenced by the imperative paradigms:
- Structured Paradigm
- Procedural Paradigm
- Object-Oriented Paradigm
Structured Programming:
Structured programming is a programming paradigm that uses structured control flow(without goto) to improve code clarity and quality.
Structured programming has three ways of combining programs — sequencing, selection, and iteration.
This paradigm makes extensive use of the block structures (sequencing), if/else(selection), and while or for loop(repetition). Edsger W. Dijkstra coined the term structured programming in his open letter on Go To Statement Considered Harmful.
For example, in the following code snippet, we are using for loop to iterate the code:
Structured Programming is also a form of Imperative programming as we explicitly loop (for, while, repeat) and change variables with explicit assignment operations at each loop. Examples of structured languages are C, C++, Java, PHP, etc. as these languages support features for — sequencing, selection, and iteration.
Advantages of Structured Programming:
Following are the few benefits of structured programming:
- The code is well organized.
- We can execute a block of code repeatedly till the given conditions match.
- Improved decision making power. Based on some conditions, we can decide to execute or not any block of code.
- The structured flow of execution using if/else and for/while. This is in contrast to Non-structured programming, which uses unstructured jumps to labels or instruction addresses using goto statements or equivalent.
Procedural Programming:
A procedural programming paradigm is derived from structured programming. It is based on the concept of the procedure call. Procedures are also known as routines, subroutines, methods, or functions.
A Procedure contains a series of instructions coupled together. We can call any procedure at any point during a program’s execution.
Computer processors provide hardware support for procedural programming through a stack register and instructions for calling procedures and returning from them.
Most of the Procedural programming languages are imperative by nature as they make explicit references to the state of the execution. The following diagram illustrates the relationship between Imperative-Structured-Procedural paradigms.
Let’s take an example of a program that will calculate the sum of first n numbers. We can write the code in the procedural programming style as follow:
Here we have divided the program into two functions. The main() act as an entry point or starting point of the execution and sumOfFirst() calculates the addition of first n numbers.
The main advantage of separating the logic for calculating the sum is that we can now reuse the same code block with different input values. Note the imperative style here; We are using the assignment operator to update the value of variable total as we iterator through the given range of numbers.
Examples of procedural languages are C, C++, Java, Kotlin, PHP, etc. as these languages support functions, methods, procedures. Using functions, we can break down the complex problem into smaller sub-problems.
Advantages of Procedural Programming:
Following are the few benefits of procedural programming:
- The code becomes reusable.
- Writing modular code is possible; this gave birth to another paradigm that is Modular Programming.
- It is easier to keep track of the control flow.
Object-Oriented Programming:
An object-oriented programming paradigm is based on the concept of objects. An object can be any real-world entity. An object has data(properties, variables) and behavior (methods). Object-Oriented paradigm took programming to the next level by supporting features like abstraction, encapsulation, inheritance, polymorphism.
There are two types of Object-Oriented programming — one is class-based, and another is prototype-based.
Class-Based:
In class-based languages, the classes are defined beforehand, and the objects are instantiated based on the classes. For example, If we want to create two new objects, one to represent an apple and another for orange, then first we need to create a class Fruit. Then we can create two instances(objects) of fruit class, one for apple and another for orange.
In class-based languages, objects are instances of classes. Java supports class-based programming.
Prototype-Based:
In prototype-based languages, the objects are the primary entities, and class doesn’t exist in it. We create new objects based on the already existing objects. These already existing objects act as a prototype for new objects.
For example, If we want to create two new objects, one to represent an apple and another for orange, then first we need to create a fruit object. Then we can create two objects from the fruit object. Here the fruit object acts as a prototype for apple and orange objects.
Delegation is a language feature that supports prototype-based programming. JavaScript supports prototype-based programming.
Advantages of Object-Oriented Programming:
Following are the few benefits of Object-Oriented programming:
- Abstraction: hide the implementation details. Generally, abstraction is achieved through abstract classes and interfaces.
- Encapsulation: It is about wrapping the implementation (code) and the data it manipulates (variables) within the same class. It is achieved using access modifiers like public, private, and protected.
- Inheritance: It is a way of creating a new class(child) from the existing class(base or parent). This mechanism enables the code reusability. As a derived class gets all the features of the base class, and it also allows to extend the functionality of a base class without modifying the existing source code.
- Polymorphism: We can have a method which has different behaviors based on the context in which it is used. We can achieve this using method overloading and method overriding.
Further Reading:
Declarative Paradigms:
Thanks for reading this blog. I hope you enjoyed this post on the imperative paradigms and learned something new. If you have any suggestions or questions, please add it in the comment below, Happy Learning 👏.