Design Principles
Software design principles are guidelines that help developers create clean, maintainable, and scalable code. These principles have stood the test of time and form the foundation of good software architecture.
SOLID Principles
SOLID PRINCIPLES
================
S - Single Responsibility Principle
"A class should have only one reason to change"
Each class does ONE thing well.
O - Open/Closed Principle
"Open for extension, closed for modification"
Add new features without changing existing code.
L - Liskov Substitution Principle
"Subtypes must be substitutable for their base types"
Child classes should work wherever parent classes work.
I - Interface Segregation Principle
"No client should be forced to depend on methods it doesn't use"
Create small, specific interfaces.
D - Dependency Inversion Principle
"Depend on abstractions, not concretions"
High-level modules shouldn't depend on low-level modules.
DRY - Don't Repeat Yourself
Every piece of knowledge should have a single, unambiguous representation. Duplication leads to bugs when you update one copy but forget the other.
BAD (DRY Violation):
====================
function calculateArea(radius) {
return 3.14159 * radius * radius;
}
function calculateCircumference(radius) {
return 2 * 3.14159 * radius;
}
GOOD (DRY Applied):
====================
const PI = 3.14159;
function calculateArea(radius) {
return PI * radius * radius;
}
function calculateCircumference(radius) {
return 2 * PI * radius;
}
KISS - Keep It Simple, Stupid
Simplicity should be a key goal in design. Choose the simplest solution that meets the requirements. Avoid over-engineering.
BAD (Over-engineered):
=====================
class UserValidatorFactory {
static create(type) {
switch(type) {
case 'email': return new EmailValidator();
case 'phone': return new PhoneValidator();
default: throw new Error('Unknown type');
}
}
}
GOOD (Simple):
=============
function isValidEmail(email) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
YAGNI - You Aren't Gonna Need It
Don't implement functionality until it's actually needed. speculative generality is a common source of complexity and technical debt.
Key Takeaways
- SOLID principles form the foundation of object-oriented design
- DRY reduces bugs and makes code easier to maintain
- KISS prevents unnecessary complexity
- YAGNI keeps you focused on current requirements