Why Are SOLID Principles Vital When Designing Software in the Object-Oriented World? | Bridge Blog

Image for post
Image for post

Designing a stable software is one of the prime objectives of any software development company. In order to ensure quality, we implement a lot of principles and patterns. Well-crafted software architecture is essential for stable and successful software.

SOLID is one of the most well-known sets of software design principles in the object-oriented design scenario. It can help you avoid common pitfalls.

The SOLID principle was introduced by Robert C. Martin, an American software engineer and instructor popularly known as ‘Uncle Bob’. The SOLID principle is a coding standard in programming.

The SOLID principles of Object-Oriented Design include the following 5 principles:

Image for post
Image for post

The SOLID helps in reducing the tight coupling of code. Tight coupling means a group of classes are dependent on one another which should be avoided for better maintainability and readability of the code. A loosely coupled code is considered a good code. Loosely coupled class objects minimize changes in your code, help in making code more reusable, maintainable, flexible, and stable.

Let’s go through each of the five principles in SOLID principles.

1. Single Responsibility Principle

Single responsibility principle states that in a well-designed application, each class (either a microservice or a code fragment) should have only one single responsibility. Responsibility is used in the sense of having only one reason to change.

For example, instead of writing everything inside a Student Class, separate the responsibilities into StudentDetails, ParentDetails, Attendance, ProgressCard, etc. Now you have a designated place for everything and you know exactly where to look when you get back to your code a year later. Same is the case with methods/ functions that are used to manipulate a class object. Each method should have a single responsibility.

2. Open/Closed principle

This principle was formulated by Bertrand Meyer in his book “Object-Oriented Software Construction”. The Open/Closed Principle states that classes, modules, microservices, and other code units should be ‘open for extension but closed for modification’.

Open for extension means we need to design the software modules/classes in such a way that the new responsibilities or functionalities should be added easily when new requirements come. Contrarily, Closed for modification means, we should not modify the class/module until we find some bugs.

We can implement this principle by following these two guidelines:

The fastest way to implement the Open-Closed Principle is to add the new functionalities by creating new derived classes which should be inherited from the original base class.

Another way is to allow the client to access the original class with an abstract interface. So, when there is a change in requirement or any new requirement comes in, then instead of modifying the existing functionality, it’s always better and suggested to create new derived classes and leave the original class implementation as it is.

3. Liskov Substitution Principle

The Liskov Substitution Principle (LSP) is the third principle of SOLID. It was formulated by Barbara Liskov in 1987 at the conference keynote talk “Data Abstraction”. The original phrasing of the Liskov Substitution Principle states that, “In a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may substitute objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.).”

In common man terms, it states that an object of a superclass should be replaceable by objects of its subclasses without causing issues in the application. Hence, a child class should never change the characteristics of its parent class (such as the argument list and return types).

The best way to implement the Liskov Substitution Principle is by implementing correct inheritance hierarchy.

4. Interface Segregation Principle

Interface Segregation Principle (ISP) applies to Interfaces instead of classes in SOLID and it is similar to the single responsibility principle. This states that “do not force any client to implement an interface which is irrelevant to them.”

Our main goal is to focus on avoiding fat interface and give preference to many small client-specific interfaces. You should prefer many client interfaces rather than one general interface and each interface should have a specific responsibility.

The violation of the Interface Segregation Principle harms code readability and forces programmers to write dummy methods that do nothing. The solution is to create smaller interfaces that you can implement more flexibly.

5. Dependency Inversion Principle

The dependency inversion principle states that: “High-level modules should not depend on low-level modules. Both should depend on abstractions.” “Abstractions should not depend on details. Details should depend on abstractions.”

In simple words, we need to decouple high-level and low-level classes. High-level classes usually have complex logic while low-level classes include data or utilities.

Conclusion

The SOLID design principles are meant to be a guide for designing software that’s easy to maintain, extend and understand. If used properly, they will help your team spend less time understanding what the code does and more time building cool features.

Originally published at https://www.bridge-global.com on January 22, 2020.

We Are A Self-Organized Squad of IT Experts

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store