Skip to content

Commit ff03cc2

Browse files
committed
content: blog
1 parent 6f74363 commit ff03cc2

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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*
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Interview Journal: #2 - C++ Rule of Five (and Three and Zero)
2+
3+
In C++, resource management is a critical skill. Unlike languages with garbage collection, C++ gives you direct control over the lifecycle of your objects. This power comes with the responsibility of correctly managing memory, file handles, and network sockets.
4+
5+
The **Rule of Five** is a guideline for modern C++ (C++11 and later) that defines which special member functions you need to implement if your class manages resources.
6+
7+
---
8+
9+
## The Evolution: The Rule of Three
10+
Before C++11, we had the **Rule of Three**. It stated that if you needed to define any of the following, you probably needed to define all three:
11+
1. **Destructor:** To release resources.
12+
2. **Copy Constructor:** To perform a deep copy.
13+
3. **Copy Assignment Operator:** To handle assignment (like `a = b`).
14+
15+
If you didn't define these, the compiler would generate default versions that perform shallow copies, leading to double-free errors or memory leaks.
16+
17+
## The Modern Standard: The Rule of Five
18+
With the introduction of **Move Semantics** in C++11, the Rule of Three expanded to the Rule of Five. We added two more functions to handle "moving" resources instead of copying them:
19+
4. **Move Constructor:** Transfer ownership of resources from a temporary object.
20+
5. **Move Assignment Operator:** Transfer ownership during assignment.
21+
22+
### The Functions at a Glance:
23+
```cpp
24+
class MyResource {
25+
public:
26+
// 1. Destructor
27+
~MyResource();
28+
29+
// 2. Copy Constructor
30+
MyResource(const MyResource& other);
31+
32+
// 3. Copy Assignment
33+
MyResource& operator=(const MyResource& other);
34+
35+
// 4. Move Constructor
36+
MyResource(MyResource&& other) noexcept;
37+
38+
// 5. Move Assignment
39+
MyResource& operator=(MyResource&& other) noexcept;
40+
};
41+
```
42+
43+
---
44+
45+
## The Rule of Zero
46+
The **Rule of Zero** suggests that you should design your classes so that you don't have to define *any* of the special five functions.
47+
48+
How? By using **RAII (Resource Acquisition Is Initialization)** wrappers like `std::unique_ptr`, `std::shared_ptr`, or `std::vector`. These classes already handle the Rule of Five correctly. If your class only contains such members, the compiler-generated defaults will work perfectly.
49+
50+
---
51+
52+
## Interview Tips:
53+
- **Why `noexcept`?** Move constructors and move assignment operators should be marked `noexcept`. This is crucial for performance, as containers like `std::vector` will only use moves during resizing if they are guaranteed not to throw.
54+
- **Copy-and-Swap Idiom:** Mentioning this idiom shows deep knowledge. It's a robust way to implement copy/move assignment operators that provides strong exception safety.
55+
- **Resource Management:** Always relate these rules back to *ownership*. Who owns the memory? When is it released?
56+
57+
---
58+
*Date: 2026-02-12*
59+
*Category: dev*
60+
*Tags: cpp, cplusplus, memory-management, rule-of-five, interview, dev*

public/posts/posts.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,5 +1149,35 @@
11491149
},
11501150
"authors": ["fezcode"],
11511151
"image": "/images/defaults/sina-salehian-HqmTUJD73mM-unsplash.jpg"
1152+
},
1153+
{
1154+
"title": "Interview Journal",
1155+
"date": "2025-01-01",
1156+
"updated": "2025-01-01",
1157+
"slug": "interview-journal",
1158+
"description": "A series dedicated to technical interview preparation, architectural patterns, and deep dives into software engineering fundamentals.",
1159+
"series": {
1160+
"posts": [
1161+
{
1162+
"slug": "solid-principles",
1163+
"title": "Interview Journal: #1 - SOLID Principles",
1164+
"filename": "/interview-journal/01-solid-principles.txt",
1165+
"date": "2025-01-01",
1166+
"category": "dev",
1167+
"tags": ["solid", "architecture", "interview", "clean-code", "dev"],
1168+
"authors": ["fezcode"]
1169+
},
1170+
{
1171+
"slug": "cpp-rule-of-5",
1172+
"title": "Interview Journal: #2 - CPP Rule of 5",
1173+
"filename": "/interview-journal/02-cpp-rule-of-5.txt",
1174+
"date": "2025-01-01",
1175+
"category": "dev",
1176+
"tags": ["cpp", "cplusplus", "memory-management", "rule-of-five", "interview", "dev"],
1177+
"authors": ["fezcode"]
1178+
}
1179+
]
1180+
},
1181+
"authors": ["fezcode"]
11521182
}
11531183
]

0 commit comments

Comments
 (0)