SOLID Principles easily explained
Reading time: 5 min
Clean code – is one of the most important commandments for many programmers. Why? Clean code not only provides a better overview, more flexibility and easy maintenance – it is also easy to understand for other programmers who work with it or deal with the code. The SOLID principles are one way of achieving exactly that. Want to try your hand at coding for the first time? Then be sure to follow the SOLID Principles. We explain what these are in this article.
The SOLID Principles form an important foundation for Clean Code. The SOLID principles were introduced by Robert C. Martin aka Uncle Bob, although not all of them originate from him. Why you should stick to it? Easy – the SOLID principles help us to do this:
- Write better code
- Build easy to understand software design
- Existing code easy to maintain and easy to extend
But first: What is a principle in software development anyway?
Principles are rules or laws that must always be adhered to during development – you could compare it to the simplest math rule: dot before dash. You could also say that they are there to solve or simplify problems. They also help you and your team to ensure that the resulting software has certain properties. The properties depend on the principle.
What do the SOLID Principles mean in full?
Single Responsibility Principle
A system has only one task
A class should only have one responsibility and there should only be one reason why you want to change the class. Do one thing and do it well! The result is an architecture that is clear, understandable and easier to maintain. If the single responsibility principle is not followed, potential maintenance problems are created in the software. This can have a negative impact on other areas and functionalities.
Advantages of the Single Responsibility Principle:
- Easy maintenance: Since you define a clear purpose for each class, it is easier to maintain because it is smaller and clearer
- Avoidance of side effects in relation to other responsibilities in the event of changes
- Better collaboration: The work in programming teams can be better divided, because each programmer can then work on a class for which he/she is responsible.
- Keeps dependencies to a minimum when reusing classes
- Small classes greatly simplify troubleshooting
- Possibility to assign clear and meaningful class names, as each class has a responsibility
- Decoupling implementations based on what is likely to change together
- Development and testing costs are reduced
Open Closed Principle
Modules should be open for extensions, but closed for modification (changes)
Basically, the Open Closed Principle is about taking a strategic approach, because it requires that written code (which has also been tested) should no longer be changed or that the software design should be constructed in such a way that changes can be supported without having to completely rebuild the system.
How can this be achieved? Through inheritance or by providing the module with an interface. This does not change the existing behavior of the module, but merely extends it with additional functions or data.
Advantages of the Open Closed Principle:
- Errors are avoided because code that has already been written is left alone
- Software becomes more maintainable
- Reuse through the design of stable interfaces
Liskov Substitution Principle
If a derived class is to be implemented, the base class must be extended without changing the behavior of the base function
To put it simply: subclasses must behave like base classes. The Liskov Substitution Principle is actually a substitutability principle. This is about interchangeability across class inheritance hierarchies: subclasses must behave like base classes. Look at the graphic with the square and the rectangle: There is a relationship between squares and rectangles: you can say every square is a rectangle, but not every rectangle is a square. As you can see, this inheritance relationship would not work in programming.
So how can you solve this in programming? Instead of the inheritance of square and rectangle, a third class is introduced from which both square and rectangle are derived. This means that the square and rectangle are only linked via the new third class.
Advantage of the Liskov Substitution Principle:
Security that an implementation can be replaced without changing its behavior
Interface Segregation Principle
Each class should be focused on one aspect or topic only
The Interface Segregation Principle deals with the separation or decoupling of classes. With the Open Closed Principle, we have learned to work with interfaces. However, interfaces are not without problems either, as they usually tend to get bigger and bigger. As a result, it is becoming increasingly difficult to provide a complete implementation, as more and more code has to be added.
The solution? interfaces in the first place and divide them into many small interfaces beforehand. This allows much more targeted interfaces to be defined externally.
Advantages of the Interface Segregation Principle:
- Simplifies reuse, testing and maintenance by splitting responsibilities
- Isolates classes from errors by introducing clear boundaries
- Enables the replacement of functions without having to understand related classes
- Reduces cognitive load by isolating topics
- Focuses attention on a single aspect
Dependency Inversion Principle
Higher level classes should not depend on lower level classes
The idea behind it: Clean up deficiencies. The principles mentioned so far have described classes, created interfaces of classes or extended classes. Now there is one more problem: the classes are still dependent on each other. So what to do? The Dependency Inversion Principle suggests using an interface to decouple two classes from each other. Thus, the first class refers to the interface and the second class implements the interface, so that the second class (because it contains the appropriate interface) can be used as a parameter in the first class.
Advantages of the Dependency Inversion Principle:
- Enables the reusability of high-level classes by inserting classes
- As implementations can be exchanged, low-level changes are facilitated
- Code simplification through standardized work with alternative classes
- Improves testability
Conclusion? Sounds solid!
As you can see, the SOLID principles simplify coding and help you to produce clean code. They also play a central role in the day-to-day work of our programmers at mmmake. Stephan Weissenberger, Unit Lead Software Development Lemures:
“The SOLID principles form the foundation of our work. They are easy to learn, but difficult to master. They are one of the first things we teach young developers in order to give them the right introduction to software development. Today, even after several years of experience, we still point out violations of these principles with a grin. The other moans about having been caught making a beginner’s mistake and corrects his work.”
Start your project with our software development!