DEV Community

Cover image for The Art of Clean Code: Mastering the SOLID Principles
Krishna kumar Shakya
Krishna kumar Shakya

Posted on

The Art of Clean Code: Mastering the SOLID Principles

In the vast world of software engineering, there's a hidden treasure known only to the true craft masters. This treasure is not gold or jewels but a guiding principle set that transforms ordinary code into a work of art. These principles, known as SOLID, are the foundation of clean, maintainable, and scalable code.

The Single Responsibility Principle (SRP):

If a Class has many responsibilities, it increases the possibility of bugs because making changes to one of its responsibilities could affect the other ones without you knowing.

Example:

"A person should have one, and only one, job to do.

Imagine if one person had to cook, serve, clean, and manage the inventory. This would be chaotic and inefficient. Instead, if one person cooks, another serves, another cleans, and another manages the inventory, everything runs smoothly.

Image description

The Open/Closed Principle (OCP):

The Open/Closed Principle (OCP), states that systems should be open for extension but closed for modification.

In simple terms, it states that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. This means that the behavior of a module can be extended without modifying its source code.

Example:

"Add new recipes to the cookbook without changing the existing ones."

Imagine a recipe book where every time you want to add a new recipe, you have to rewrite the entire book. Instead, you can just add new recipes on new pages without touching the existing ones.

Image description

The Liskov Substitution Principle (LSP)

The Liskov Substitution Principle (LSP), states that substitutes should seamlessly replace the originals without any issues.

Example:

"Substitutes should work just as well as the originals."

Imagine a toy store where you can replace any toy car with another toy car, and it should still work perfectly on the same track. If you replace a toy car with a toy boat, it won’t work on the car track.

Image description

The Interface Segregation Principle (ISP)

The Interface Segregation Principle (ISP), states that no one should be forced to do things they don't need. It believes in simplicity and specificity.

Example:

"Only do what you need to do."

Imagine a smartphone that forces you to use all its apps, even the ones you don’t need. It’s better if you can use only the apps you want.

Image description

The Dependency Inversion Principle (DIP)

The Dependency Inversion Principle (DIP), states that high-level operations should not depend on low-level details, but on abstractions.

Example:

"Focus on the big picture, not the details."

Consider a lamp and a switch. If the switch directly controls the bulb, any change in the bulb would require changing the switch too. Instead, if the switch controls any device through an interface, it works with any type of bulb or device.

Image description

Conclusion
As our journey comes to an end, we realize that the SOLID principles are not just rules but the essence of writing clean and maintainable code. By following these principles, we can create software that is robust, flexible, and easy to understand.

Remember, the path to mastery is not easy, but with Sir Simple, Lady Opena, Lord Liskov, Sir Interface, and Lady Dependence as your guides, you are well on your way to becoming a true code artisan.

Conclusion

  • Adherence to SOLID Principles: The SOLID principles are crucial for writing clean, maintainable, and scalable code. Each principle serves a specific purpose that collectively enhances software quality.

  • Single Responsibility Principle (SRP): Ensures each class or module has only one responsibility, improving code readability and maintainability.

  • Open/Closed Principle (OCP): Promotes designing systems that are open for extension but closed for modification, reducing the risk of introducing bugs during updates.

  • Liskov Substitution Principle (LSP): Guarantees that subclasses can replace their base classes without altering the correctness of the program, ensuring consistency and reliability.

  • Interface Segregation Principle (ISP): Advocates for creating specialized interfaces that are tailored to the needs of the clients, avoiding unnecessary dependencies and improving modularity.

  • Dependency Inversion Principle (DIP): Encourages depending on abstractions rather than concrete implementations, facilitating a flexible and adaptable architecture.

  • Path to Mastery: Mastering these principles requires practice and dedication, but their application is essential for becoming a skilled software developer capable of creating robust and adaptable systems.

Feel free to leave your comments or suggestions below! Your feedback is invaluable and help me improve.

Top comments (11)

Collapse
 
kelvinriker profile image
Kelvin Riker

Nice writeup and very usefull information.

One tip, could be to add "wrong" approach first and then correct after.
It highlights the differences and makes it even more clear.

Collapse
 
optim_scaria profile image
Alen Scaria

Agree!

Collapse
 
martinbaun profile image
Martin Baun

Great writeup! The thing to really understand, IMO, is state. How many states can your code be in, where is that state stored, how can it be corrupted, and what state are your external dependencies in if someone pulls the plug on your computer at any given line of code?

Collapse
 
whattheportal profile image
WTP | WhatThePortal.com

Good to see these occasional refreshers on important concepts.

All too often when getting into the weeds of product development we can forget the underlying motivation behind some patterns & behaviors.

Just need a linter to enforce now so humans don't have to!

Collapse
 
watcherx profile image
Bobby

Interface Segregation Principle (ISP) example is wrong. Chef should also extend eatable, else your eat method is out of contract.

Collapse
 
littlerobotguys profile image
MicahWoodrowPhillips

I feel like your discussion around ISP is nearly the same one as in SRP. It's not that it's exactly wrong, but they both "Only do what you need to do." The difference is that ISP is not borrowing from other interfaces that they don't use. ISP is more about not adding clutter, where SRP is more about sticking to one focus, which are similar, but different.

Collapse
 
andrel profile image
AndrΓ© Portela

The way that you explain, is fantastic!

Collapse
 
alexandermirzoyan profile image
Alex Mirzoyan

Nice explanation!

But at the ISP principle's code example I think there is a problem as the Eatable class is not being used anywhere.

Collapse
 
obie_kenobie profile image
Eren Jaeger

Interesting read !

Collapse
 
kocreative profile image
Kat

Thanks for this! Very interesting read and well explained.

Collapse
 
rafaeljohn9 profile image
JohnKagunda

SOLID made easier πŸ‘, thanks