|
| 1 | +# Interview Journal: #1 - SOLID Principles |
| 2 | + |
| 3 | +In the world of software engineering, specifically object-oriented design, the SOLID principles are the bedrock of clean, maintainable, and scalable code. Coined by Robert C. Martin (Uncle Bob), these five principles help developers avoid "code rot" and build systems that are easy to refactor and extend. |
| 4 | + |
| 5 | +As this is the first entry in my **Interview Journal**, I want to dive deep into these principles, explaining them with clear examples and why they matter in a professional environment. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 1. Single Responsibility Principle (SRP) |
| 10 | +> "A class should have one, and only one, reason to change." |
| 11 | + |
| 12 | +This is perhaps the most misunderstood principle. It doesn't mean a class should only have one method. It means a class should be responsible for one *actor* or one specific part of the functionality. |
| 13 | + |
| 14 | +**Bad Example:** |
| 15 | +A `User` class that handles both user data and saving that data to a database. |
| 16 | +**Good Example:** |
| 17 | +A `User` class for data and a `UserRepository` class for persistence. |
| 18 | + |
| 19 | +## 2. Open/Closed Principle (OCP) |
| 20 | +> "Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification." |
| 21 | + |
| 22 | +You should be able to add new functionality without changing existing code. This is usually achieved through interfaces and abstract classes. |
| 23 | + |
| 24 | +**Scenario:** |
| 25 | +If you have a `DiscountService`, instead of using a giant `switch` statement for every new discount type, you define a `DiscountStrategy` interface. Adding a new discount means adding a new class, not touching the `DiscountService`. |
| 26 | + |
| 27 | +## 3. Liskov Substitution Principle (LSP) |
| 28 | +> "Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program." |
| 29 | + |
| 30 | +If `Class B` is a subclass of `Class A`, you should be able to pass `B` anywhere `A` is expected without the program breaking. |
| 31 | + |
| 32 | +**Common Violation:** |
| 33 | +The classic Square-Rectangle problem. If a `Square` inherits from `Rectangle` but overrides the `setHeight` to also change the `width`, it breaks the expectations of a `Rectangle` user. |
| 34 | + |
| 35 | +## 4. Interface Segregation Principle (ISP) |
| 36 | +> "Many client-specific interfaces are better than one general-purpose interface." |
| 37 | + |
| 38 | +Clients should not be forced to depend on methods they do not use. Split large interfaces into smaller, more specific ones. |
| 39 | + |
| 40 | +**Example:** |
| 41 | +Instead of a `SmartDevice` interface with `print()`, `fax()`, and `scan()`, create `Printer`, `Fax`, and `Scanner` interfaces. A basic printer shouldn't be forced to implement a `fax()` method. |
| 42 | + |
| 43 | +## 5. Dependency Inversion Principle (DIP) |
| 44 | +> "Depend upon abstractions, [not] concretions." |
| 45 | + |
| 46 | +1. High-level modules should not depend on low-level modules. Both should depend on abstractions. |
| 47 | +2. Abstractions should not depend on details. Details should depend on abstractions. |
| 48 | + |
| 49 | +**In Practice:** |
| 50 | +Instead of a `Store` class instantiating a `StripePayment` object directly, it should depend on an `IPaymentProvider` interface. This allows you to swap Stripe for PayPal without changing the `Store` logic. |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Why SOLID Matters in Interviews |
| 55 | +Interviewers look for more than just "I know what the letters stand for." They want to see: |
| 56 | +- **Trade-offs:** When *not* to over-engineer with SOLID. |
| 57 | +- **Real-world application:** Can you spot a violation in a code review? |
| 58 | +- **Architectural Thinking:** How these principles lead to patterns like Strategy, Factory, and Dependency Injection. |
| 59 | + |
| 60 | +In the next journal entry, we'll look at **Design Patterns** and how they implement these SOLID foundations. |
| 61 | + |
| 62 | +--- |
| 63 | +*Date: 2026-02-12* |
| 64 | +*Category: dev* |
| 65 | +*Tags: solid, architecture, interview, clean-code, dev* |
0 commit comments