The Best Paradigm
In programming like any other art has different categories i.e. paradigms. There are many like functional, object oriented, event driven, procedural etc.
All this paradigms have a cult like following among the people who use them. This is mostly due to the paradigms being linked to the programming languages they use. In today's blog we are going to look at the functional, object oriented and procedural paradigms. There other paradigms like event driven, declarative, structured query language paradigm etc.
1. Procedural paradigm
Being a newbie in programming, the first language I learnt was C. This is in the procedural paradigm as it is divided into functions. The main function and other functions.
In procedural paradigm code is divided into functions (also called routines or sub-routines in other languages and should not be confused with functional programming paradigm). The most popular procedural language in this paradigm is C. There are other languages like python, JavaScript and C++.
The functions form the basis of code. Each function performs code in a defined sequence after being called. In C the main function is the entry point to the program. The functions are arranged in a certain hierarchical order depending on when they would be executed. Each code block is enclosed in its own function.
Each function may contain its own variables or their maybe other variables which are global and can be used throughout the entire program. Some variables i.e. those which do not change may be made constant in some programming languages like C and JavaScript.
The paradigm also includes the use of control structures i.e. conditions and loops.
Advantages
- Simplicity: Due the use of functions, they user is able to learn how to program each function and also how and what each task performs. It is easy for beginners to start with.
- Readability: A linear flow makes code easy to follow.
- Efficient for Simple Tasks: Works well for smaller or well-defined problems.
- Wide Support: Supported by many programming languages (e.g., C, Pascal).
Disadvantages
- Scalability: The simplicity and readability of code comes at a great disadvantage when it comes to scaling. This is as the project increases and the requirements increase the number of functions also increase making hard to keep up with the code.
- Limited reusability: Code can only be reused less as compared to other paradigms. The only reusable elements are the constant variables
- Can not be used to model real life phenomenon: Unlike Object Oriented paradigm the procedural paradigm can not be used to model things.
While the procedural paradigm has its ups and down, it is one of the easiest to learn. For new beginners I would advise this.
2. Object Oriented Paradigm
The second language I learnt in my programming language journey was Java. It was vastly different from the C. While it retained some characteristics of C like control structures it had classes. This programming language paradigm has been implemented in almost all the modern programming languages like JavaScript, SmallTalk, Python, Ruby, PHP just to mention a few.
This paradigm was meant as an improvement on the existing procedural paradigm. In object oriented paradigm, it focuses on making the code models of the real world. The main features are classes, objects, inheritance, polymorphism and abstraction. One of the earliest adopters of this paradigm was C++. While it may not be truly Object Oriented like SmallTalk and Java, it utilised some of the C procedural with object orientation (its earliest name was C with classes).
In Object Oriented paradigm(OOP), classes form the template of the model. Each code is defined in the class. In Java the main class is the entry point into the program. Objects are the instances of a class. While the class defines the structure of the object, it is the object that is manipulated. Since the code is divided into classes, this makes the program modular in nature.
Each object has methods and attributes. Attributes are like the variables in procedural while methods are the functions in procedural programming which describe the behaviour of an object. The attributes and methods have access modifies like private, public and protected which define how they can be accessed. There are also special methods and attributes that are static i.e. can be used without instantiating a class or object.
Polymorphism is a feature of OOP programs that allows a method to behave differently depending on the context. Like a method sound in class animal will react differently for a dog and a duck. Inheritance, allows a child like dog to inherit properties from a parent class like animal. In abstraction, the finer details of how the code works that are useless to the user hidden from the user.
Advantages
- Real world modelling: This is through classes and objects which try to get as much information and represent it in class form
- Code Reusability: This is achieved by polymorphism and inheritance
- Maintainability: This is through its modular design.
- Collaboration: Since each code is made up of classes, each team can work on a class or module simultaneously
Disadvantages
- Complexity: The structure of classes and objects can be overly intricate for small or simple programs.
- Performance Overhead: Object creation and garbage collection can slow down execution compared to procedural programming.
- Learning Curve: OOP concepts like polymorphism and abstraction can be challenging for beginners.
3. Functional Paradigm
The birth of lisp in 1958, symbolised the birth of the functional programming paradigm. While it was in 1958 that the paradigm was implemented in computers, this is paradigm is one of the oldest as it has its roots in mathematics.
To be honest, I don't have much experience with this paradigm mostly due to the weird. But some of the key concepts include pure functions, immutability, high order functions, side effects and recursion.
The functions are said to be pure as they always produce the same output for the inputs and the outputs are not modified after being produced i.e. a square function always return the square of the number entered. There are presence of immutable variables which are not changed. For high order functions, the function can be used as the input for another function. They are no side effects as the functions do not modify the external environment.
Despite the current hype around this paradigm, they are used in special use cases like for research and high precision systems like banking. Some of the popular languages include prolog, haskell, clojure and F#. Since I know little about this I will not go into details of the advantages and disadvantages of each.
4. Multi-Paradigm
For most languages, both new and current, the focus is on trying to incorporate the best of each world in the programming languages. For example modern programming languages like python provide the procedural paradigm and object oriented paradigm. In addition there are application of functional paradigms through lambda functions and functions like map.
Conclusion
In the end , there is no one paradigm that solves all the problems and is liked by all programmers. Each paradigm has its own uses cases, strengths and weakness. The thing to keep in mind is to ensure that you understand the paradigm and its ability to help complete the task at hand.




Comments
Post a Comment