After reading Clean Architecture, I've had a trouble understanding differences of each programming paradigm;
- Structured Programming (SP)
- Object-oriented Programming (OOP)
- Functional Programming (FP)
Let me share a summary of Clean Architecture Part II, Starting with the Bricks: Programming Paradigms.
Firstly, what is,
A Programming Paradigm?
It is a programming discipline, used to decide which form of programming structure (SP, OOP, FP) to use.
Each paradigm places a constraint on a programmer.
NOTE: "Paradigm" is pronounced as par-uh-dahym NOT as par-uh-dig-uhms.
Disciplines imposed by each paradigm
-
Structured Programming
A direct transfer of control Example: goto statement is discouraged or removed. Instead of using a goto statement for an unconstrainted control flow, programmers are forced to use (now familiar) constructs such as if/else, for/while loops.
-
Object-oriented Programming
An indirect transfer of control Example: Function pointers are eliminated. OOP provides a plug-in architecture with a use of polymorphism. Polymorphism also enables a programmer to change the code dependencies with Dependency Inversion Principle.
-
Functional Programming
A variable assignment Example: Cannot assign a new value to a variable.
What problem(s) each paradigm solves
-
Structured Programming
SP causes noodlers (preventing spaghetti codes) to become an endangers species How? By discouraging the use of goto and encouraging functional decomposition and use of data structures.
-
Object-oriented Programming
Complete control over code dependencies and flows. How? By using dependency inversion with the use of polymorphism.
-
Functional Programming
All race/deadlock conditions, and concurrent update problems. How? Problems mentioned above are caused by variable mutation. FP prevents variable assignment, thus those problems can't occur.
Programming Paradigm Use Cases
Initially, I thought about when to use each paradigm. When you consider a software architecture as a whole, it makes more sense to use each one appropriately.
So as an example, write low-level methods with SP principles, and separate immutable components (written with FP) from mutable components to mitigate resource contention and control flows with OOP.
Surprises 🎉
- Science theories and laws are not provable, only falsifiable.
- Encapsulation, Inheritance, and Polymorphism concepts are not introduced in OOP! - These concepts were available in C, but the usages were unsafe and OOP made it safer/easier to use.
- Access modifiers, public``private and protected in OOP are just a hack necessitated by technical reasons! - C header files hid member variables but C++ header files had to expose member variables because C++ compilers have to know the size of the instances of each class. Even worse, modern OOP languages such as C#/Java do not separate a class declaration from its implementation therefore, those modifiers are necessary evils.
- In FP, variables do NOT vary!
Conclusion
- Each paradigm tells a programmer what NOT to do.
- Clean Architecture is a book written by Robert C. Martin, aka Uncle "Bob".
- Coding Blocks guys talk about it in more detail in Episode 69 – Clean Architecture – Programming Paradigms.