A quick overview of four fundamental principles for writing clean, maintainable, and scalable code: SOLID, DRY, KISS, and YAGNI.
A set of five object-oriented design principles introduced by Robert C. Martin (Uncle Bob):
S - Single Responsibility Principle (SRP)
A class should have only one reason to change.
➤ One class = One responsibility.O - Open/Closed Principle (OCP)
Software entities should be open for extension, but closed for modification.
➤ Add new features without modifying existing code (use abstraction and polymorphism).L - Liskov Substitution Principle (LSP)
Subtypes must be substitutable for their base types.
➤ A subclass should behave like its parent class without breaking the program.I - Interface Segregation Principle (ISP)
Many client-specific interfaces are better than one general-purpose interface.
➤ Classes should not be forced to implement methods they don’t use.D - Dependency Inversion Principle (DIP)
Depend on abstractions, not on concrete implementations.
➤ High-level modules shouldn’t depend on low-level ones.
"Every piece of knowledge must have a single, unambiguous representation in the system."
- Avoid duplicating code or logic.
- Centralize reusable logic in a single place.
✔️ Good:
functioncalculateDiscount(price: number){returnprice>100 ? price*0.1 : 0;}“Simplicity is key to maintainability.”
- Prefer simple and clear solutions over overly abstract or complex ones.
- Don’t over-engineer unless it’s necessary.
✔️ Good:
functionadd(a: number,b: number){returna+b;}“Don’t implement functionality until it’s actually needed.” • Avoid adding speculative features. • Focus only on the current requirements to keep the codebase lean.
⸻
- SOLID → Helps structure complex systems.
- DRY → Keeps logic consistent.
- KISS → Avoids unnecessary complexity.
- YAGNI → Prevents writing unused code.