Introduction to Programming Paradigms

What are they?
Refers to a stylistic approach to programming as way of organizing code in a meaningful manner. These approaches are not defined by the languages or rules set by the creators of programming languages, instead are based on a sub-culture of people who have come up with these ideals and principles.
Why do we need them?
Understanding different styles of programming will give us an abstract way in which we can view the development of applications and other forms of technology, thus fostering new ways of development, they provide a tool to approach programming, encourages open-ended thinking in terms of development process, and allows for flexibility in programming styles which removes the idea of thinking within a box.
Types
Imperative
Refers to the developing programs that follow an explicit step-by-step instructions to the computer, of which is executed in chronological order.
The term imperative in this case states how the computer should perform each task.
The focus of imperative programming, describes how a program operates step by step.
Consider this example of code where you filter numbers greater than 4 from a list:
nums = [5,6,1,2,3,10,9]
result = []
for i in range(0, len(nums)):
if nums[i] > 4:
result.append(nums[i])
print(result)
Internally the process is as followed:
Iterate through each element
Compare each element the target value (4)
If greater than 4, add it to the result list
In this example everything is explicitly controlled
Procedural
A derivative of imperative programming that introduces the utilization of subroutines.
The purpose is do improve modularity and maintainability of code, foster organization due to the use of subroutines, reusing of code and providing abstraction.
Let's consider a program that asks the user to enter their details (name, age & nationality)
// Imperative programming
print("Welcome to the system")
name = input("Enter name: ")
age = input("Enter age: ")
nationality = input("Enter nationality: ")
print(f"Hello", name , " you are ", age , " years old and are from ", nationality)
//procedural
def getDetails():
name = input("Enter name: ")
age = input("Enter age: ")
nationality = input("Enter nationality: ")
print("Hello", name, "you are", age, "years old and are from", nationality)
The difference between the two obviously lies in the use of the subroutines and not, however one is more optimized to be resusable and modular, where as the other is more sequential and will only run once.
Functional
Functional programming refers to the extensive use of procedural programming, only that it utilizes return statements, that way the results of these subroutines are able to be stored in variables after their calling.
It treats the use of subroutine as a necessity.
Consider this code which gets the first and the last name from a user in a function called getName()
def getName():
forename = input("Enter forename:")
surname = input("Enter surname:")
return f"{forename} {surname}"
def getAge():
age = input("Enter your age:")
return age
def getNationality():
nationality = input("Enter nationality: ")
return nationality
def main():
name = getName()
age = getAge()
nationality = getNationality()
return f"Hello, I am {name}, I'm {age} years old, and I'm from {nationality}"
if __name__ == "__main__":
main()
In this example the program automatically runs the main function, of which that main function will call the three other functions that returns values stored in 3 variables and will return a sentence consist of the said variables.
Declarative
A paradigm focused on the indication of what the result should be rather than the steps to go towards that result. It's a direct contrast to imperative programming.
In this code block, the goal is to keep the numbers greater than 5.
#Imperative programming:
nums = [1,4,3,6,4,2,9,2]
result = []
for num in nums:
if num > 5:
result.append(num)
print(result)
#Declarative programming
nums = nums = [1,4,3,6,4,2,9,2]
result = [ num for num in nums if num > 5]
The two styles achieves the same result, however imperative is more explicit with the steps shown, whereas declarative is more implicit using list comprehension to get the values inside the array (result) to shorten the steps shown.
The idea is to hide the implementation details and focuses on the desired outcome.
Examples of declarative styles include:
SQL Queries
Functional Pipelines
List Comprehension
Object Oriented
Object oriented programming, one the most popular and widely used paradigms.
It's a style of programming that utilizes classes and objects alongside further detailed concepts to represent entities and their relationships.
Classes - Refers to a blueprint of an entity, it contains the behavior and characteristics of said entity through the use of attributes, methods.
Objects - Are instances of the class, they utilize the class to make themselves alongside their respective attributes.
Relationships of classes and objects are also made using concepts such as:
Inheritance
Polymorphism
Encapsulation
Abstraction
The bigger picture
Programming paradigms shape the way we write code, thus influence the performance and development of systems, apps, and etc, it's up to us what we choose whether we want something sequential (imperative), structured using procedural/functional, result driven with declarative or even have a mix of multiple styles.
