Skip to main content

Let’s be real for a second—when we first dive into programming, most of us just want to make things work. I remember my early days of coding when I started as a junior developer. I was proud just to get a function to run without throwing errors. I wasn’t really thinking about long-term maintainability or how readable my code was. But as time goes on, you quickly realize that your future self (or the poor souls who inherit your code) will either love you or hate you based on how clean and scalable your code is.

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. Code for readability. – Martin Golding

Enter the SOLID principles: a set of five design guidelines that, once you start using regularly, will make your code a joy to work with and not just for you but for anyone who works on it. But don’t worry, I’m not going to dive into textbook definitions. Instead, I’ll walk you through how I use each principle in my day-to-day programming in a way that’s fun, engaging, and hopefully relatable.

1. Single Responsibility Principle (SRP)

“Every class should have one, and only one, reason to change.”

Think of SRP like this: each class should be focused, like that one colleague who only ever talks about their hobby and knows it inside out. The moment you start adding multiple responsibilities to a class, it’s like giving that colleague multiple jobs—and trust me, it won’t end well.

How I Apply SRP:
In my early days, my classes were like those infamous all-in-one kitchen tools that promise to chop, dice, blend, and fry—but end up doing everything poorly. Now, if I find myself writing a class that handles both business logic and database operations, I take a step back. I break things down. The goal is to have a clean,focused class that does one thing well and leaves the rest to other classes.

Pro tip: When you are creating a class and it begins to seem like it is trying to do too many things, it is time to refactor.

2. Open/Closed Principle (OCP)

“Software entities should be open for extension, but closed for modification.”

This one sounds fancy, but it’s really about not messing with existing code to add new features. Picture this: you’ve got a nice ,clean ,well-tested class,and a new feature request comes in. The rookie move would be to open that class and start adding in new functionality. Instead, SOLID tells us to extend that class without changing it.

How I Apply OCP:
Say I’ve got a PaymentProcessor class. If I need to add a new payment method, I don’t touch the core class; I extend it. Maybe I create a new CryptoPaymentProcessor class that inherits from PaymentProcessor. This way, I’m not breaking anything that already works, and my new class handles the extra functionality.

Pro tip: If you find yourself repeatedly modifying the same class for new features, you’re violating OCP. Look for ways to extend instead!

3. Liskov Substitution Principle (LSP)

“Objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program.”

The LSP sounds like a mouthful, but it’s really just about making sure that subclasses behave like their parents. It’s like when your parents leave you in charge for the weekend—you should be able to handle things as they would (within reason).

How I Apply LSP:
I learned this one the hard way. I once created a Bird class with a fly() method and then extended it for a Penguin. Of course, the Penguin can’t fly, and suddenly my program was crashing because I’d broken LSP. The fix? Refactor the Bird class into something like FlyingBird and NonFlyingBird to respect the differences without violating LSP.

Pro tip: If you find yourself writing “if” statements in a subclass to handle a case that breaks the superclass contract, you’re in LSP territory. Refactor!

4. Interface Segregation Principle (ISP)

“Clients should not be forced to depend on interfaces they do not use.”

I like to think of ISP as the “don’t overload people with unnecessary stuff” principle. It’s like when you’re in a meeting, and someone starts explaining a part of the project that has absolutely nothing to do with your work. You’re left wondering, “Why am I here?”

How I Apply ISP:
If I create an interface and it’s huge, covering every possible method I think I might need, I’m probably violating ISP. Instead, I now focus on creating smaller, more specific interfaces. For example, if I have a Printer interface, I don’t force it to include a Scan() method just because some printers scan. I’d create a Scanner interface for that instead.

Pro tip: When writing an interface, ask yourself, “Does every implementation of this interface need to know about this method?” If the answer is no, break it up.

5. Dependency Inversion Principle (DIP)

“Depend on abstractions, not concretions.”

DIP is like the golden rule of clean architecture. Instead of depending on specific implementations (which can change), you depend on abstractions (like interfaces), so you can swap out implementations without breaking everything.

How I Apply DIP:
I used to write code that depended on specific classes all the time, like using a MySQLDatabase class directly. But what happens if the database changes to PostgreSQL? Boom—lots of refactoring! Now, I depend on an abstract Database interface, and the actual implementation (MySQLDatabase or PostgreSQLDatabase) gets injected where needed. This keeps my code flexible and easier to maintain.

Pro tip: If you find yourself writing new ClassName() all over your code, you’re probably violating DIP. Use dependency injection instead!

Conclusion: Making SOLID Part of Your Daily Coding

Using the SOLID principles daily might sound like extra work, but once you get the hang of it, it becomes second nature. Start small. The next time you’re writing or reviewing code, ask yourself if you’re violating any of these principles. And trust me, your future self—and your fellow developers—will thank you.

At the end of the day, writing clean,maintainable code is what separates good developers from great ones. And with SOLID principles, you’re well on your way to becoming the latter.


So, what are your thoughts? Have you bumped into any of these principles lately? Let’s chat in the comments!

Explore Other Resources

February 14, 2024 in Case Studies, Product Engineering

CLAS – A system that integrates Hubspot, Stripe, Canvas

About This Project Ziplines is a series A-funded ed-tech startup with one goal—helping students attain the real-world skills they need to thrive in careers they love by partnering with universities.…
Read More
September 9, 2024 in blog

Gentle Introduction to Elasticsearch

Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents. Elasticsearch is developed…
Read More
May 17, 2024 in blog

How to fix “OAuth out-of-band (OOB) flow will be deprecated” error for Google apps API access.

Migrate your OAuth out-of-band flow to an alternative method. Google has announced that they will block the usage of OOB based OAuth starting from January 31, 2023. This has forced…
Read More

Leave a Reply