SOLID নীতি ও Clean Code

লেসন ৬ / ৫০

SOLID — OOP-এর 5 নীতি

S — Single Responsibility

Class-এর শুধু একটি change-এর কারণ থাকবে।

// ❌ Bad
class User {
    void Save() {} void Report() {} void Email() {}
}
// ✅ Good
class User {} 
class UserRepository {}
class UserReportService {}
class EmailService {}

O — Open/Closed

// ✅ Polymorphism — extension open, modification closed
abstract class Shape { abstract double Area(); }
class Circle : Shape { override double Area() => π * r * r; }
class Square : Shape { override double Area() => s * s; }

L — Liskov Substitution

Subclass parent-এর জায়গায় ব্যবহার করলে behavior break হবে না।

// ❌ Penguin : Bird { Fly() { throw; } }   // LSP violation
// ✅ IFlyable interface আলাদা

I — Interface Segregation

Client-কে অপ্রয়োজনীয় method implement করতে বাধ্য না।

// Big interface ভেঙ্গে: IWorkable, IEater, ISleeper

D — Dependency Inversion ⭐

// ✅ Inject interface, not concrete
class OrderService {
    OrderService(IOrderRepository repo) {...}
}

Clean Code Principles

  • DRY — Don't Repeat (3 বার copy = function)
  • KISS — Keep It Simple
  • YAGNI — অগ্রিম feature না
  • Boy Scout Rule — cleaner রেখে যান
  • Fail Fast

Naming

❌ var d = 5;
✅ var daysSinceLogin = 5;

❌ class Manager
✅ class OrderProcessor

Function Rules

  • Small (10-20 lines), do one thing
  • 3-এর কম parameter, no side effects
  • Verb name (calculateTotal)

Refactoring Patterns

Extract Method, Extract Class, Replace Conditional with Polymorphism, Replace Magic Number।

Technical Debt

প্রতি sprint-এ 10-20% time refactor-এ রাখুন।