OOP :
A programming approach that treats real-world things as objects, extracts the features and functions you want from those objects, and moves the real world into a program.
OOP pros :
- Code becomes more reusable.
- It's easy to maintain.
- Code becomes more concise.
OOP cons :
- Processing takes longer.
- You have to invest a lot of thought and time into the design.
The 5 OOP principles S.O.L.I.D :
SRP :
A class should have only one purpose and should change for only one reason.
OCP :
It should be open to extension but closed to modification.
(Extension: extend functionality by adding new behavior, modification: you shouldn't change existing code, you should add behavior)
LSP :
Replacing an object of a supertype with a subtype should still work correctly.
(It doesn't mean it should compile correctly, it means it should work according to the program's design logic)
ISP :
Several interfaces for different purposes are better than one general-purpose interface.
DIP :
Low-level modules should depend on high-level modules.
(Low-level module: an abstract module far from IO, related to business)
(High-level module: a concrete module close to IO, related to HTTP, DB, cache, etc.)
Procedural programming vs object-oriented programming :
Procedural programming :
- Programming that values sequential processing, like water flowing downward
- It resembles the computer's processing structure, so it runs fast.
- If the order of the code changes, the result easily changes too.
- C is the typical example.
Object-oriented programming :
- Programming that treats real things as objects and moves the real world into code
- It runs slower than procedural programming.
- You can use techniques like polymorphism, encapsulation, and inheritance.
- JAVA is the typical example.