TechnicalMedium OOP & Design
What are the SOLID principles?
Asked for Backend Developer, Full-Stack Developer
Why they ask this: Mid-level and good junior candidates are expected to know how to keep code maintainable.
Answer
- S, Single Responsibility: a class should have one reason to change. Do not mix validation, payment and email sending in one class.
- O, Open/Closed: open for extension, closed for modification. Add a new
KhaltiGatewayclass instead of adding anotherifto existing code. - L, Liskov Substitution: a subclass must work anywhere its parent is expected, without surprising behaviour.
- I, Interface Segregation: prefer several small interfaces over one large one, so classes do not implement methods they do not need.
- D, Dependency Inversion: depend on abstractions (interfaces), not concrete classes, and inject them, for example through Laravel's service container.
Do not just recite the list: give one real example from your own project of a principle you applied, or one you broke and later fixed.
Can you explain this in your own words?