SOLID - Principle

Harsh Dalal

Wed, 22 May' 24

grocery app

In the vast world of software development, there's a set of guiding principles known as SOLID. But what do these principles really mean, and why should you care, especially if you're not a tech whiz? Well, think of SOLID as the building blocks of sturdy, reliable code, and let's explore them using relatable everyday scenarios.

1. Single Responsibility Principle (SRP)

Imagine you have a friend, Sarah, who's an amazing event planner. She specializes in organizing birthday parties. Now, Sarah doesn't try to do everything herself. She knows her strength lies in planning and coordinating, so she focuses solely on that. She doesn't bake the cake, decorate the venue, and entertain guests all by herself. Instead, she delegates these tasks to experts—a baker, a decorator, and maybe a magician or clown.

In coding terms, SRP means that each "friend" (class or module) in your codebase should have one specific job, just like Sarah focuses on planning. By keeping responsibilities clear and separate, your code becomes easier to understand and maintain, just like Sarah's parties are well-organized and stress-free.

2. Open/Closed Principle (OCP)

Imagine you have a closet with a basic set of shelves for storing your clothes. Over time, you accumulate more items—like hats and shoes. Instead of reconstructing the entire closet to accommodate these new items, you add new shelves, hangers, or storage bins to the existing setup.

In coding terms, OCP means that your existing code should be "closed" for modification, meaning you shouldn't have to change it. However, it should be "open" for extension, allowing you to add new functionality. Just like you extend your closet by adding new storage solutions, you extend your code by adding new classes or methods without altering the existing, stable codebase. This approach maintains the integrity and reliability of your original setup while enabling growth and adaptability.

3. Liskov Substitution Principle (LSP)

Think of your phone charger—a universal USB cable. You can use this cable to charge various devices—phones, tablets, cameras—without worrying about compatibility. That's because all these devices adhere to a common charging standard, allowing for seamless interchangeability.

In coding, LSP means that you can substitute a parent class with its child class without affecting the behavior of your program. Just like your USB cable works with different devices, code built following LSP can seamlessly swap one object for another, maintaining the program's integrity.

4. Interface Segregation Principle (ISP)

Imagine you're at a buffet with a wide array of dishes—salad, sushi, steak, and dessert. Now, each food station offers a specialized menu tailored to its cuisine. You wouldn't expect to find sushi options at the steak station, right?

In coding, ISP means that interfaces should be specific to the needs of the client. Just as each food station offers a focused menu, interfaces should cater only to the requirements of the modules that use them. This prevents unnecessary dependencies and keeps your codebase clean and organized.

5. Dependency Inversion Principle (DIP)

Think of a car with a GPS system. The GPS doesn't need to know the intricate details of the car's engine or the exact route you'll take. It simply provides directions based on your input, abstracting away the complexities of navigation.

In coding, DIP means that high-level modules should not depend on low-level modules but on abstractions. Just like the GPS abstracts away the details of the car, DIP encourages modular design and decoupling, making your code more flexible and easier to maintain.

In essence, SOLID principles are like guiding stars that steer developers towards writing cleaner, more maintainable code. By understanding these principles through relatable analogies, you too can appreciate the art of crafting software that stands the test of time. So, whether you're planning a party or writing code, remember: keeping it SOLID leads to success!