Robert C. Martin introduced SOLID principles in the early 2000s. Yet today, after two decades after his explanation about the principles, they still serve the purpose of designing well-thought-out and flexible programs. The five fundamental principles are:
Single Responsibility Principle
‘There should never be more than one reason for a class to change.’
Simply put, a class has one responsibility and do it well.
‘A module should be open for extension but closed for modification.’
Simply put, favour composition over inheritance. And you think about composition while writing your code, the cleaner the tests become. For example, a mock function when the tests are being run instead of a real function. It’s easy to swap them when you have composition in your mind.
‘Subtypes must be substitutable for their base types.’
Simply put, the objects of a class should be replaceable with objects of a subclass without breaking the application.
‘Classes that implement interfaces, should not be forced to implement methods they do not use.’
Simply put, I prefer various small and straightforward interfaces over one, God-like interface.
Dependency Inversion Principle
‘High-level modules should not depend on low-level modules, rather both should depend on abstraction.’
Simply put, I prefer to inject a high-level function into a low-level function by passing it in a low-level function argument. That way, a low-level function has nothing to do or care about a high-level function but use it.
Other Acronyms Apart From SOLID Are Relevant Still Today.
Don’t Repeat Yourself. Don’t repeat yourself while coding, testing and documenting.
You Aren’t Going To Need It. You aren’t going to need to write a separate function to sum up the row numbers when the client says she wants a total number only, for example. It’s loosely tied by MVP, Minimum Viable Product.