Software design principles represent a set of guidelines that helps us to achieve a good design. These principles are key to create clean and modular design.
A good programmer should always strive for highly cohesive and loosely coupled solution, code or design. For that, one should be fully aware of OOP and SOLID design principles, and should be able to apply those design principles in coding.
Here, we've listed the top 10 important object oriented desing principles that we've found so useful and frequent while designing the software solution. First five principles are SOLID desing princples and rest are additional to them.
1. Single Responsibility Principle ( SRP)
According to this design principle, a class should have only one responsibility. That means, there should never be more than one reason for a class to change. Providing more than one responsibilty or functionality in one class introduces tight coupling between two functionality and even if you change one functionality there is chance you broke coupled functionality, which requires unnecessary another round of testing to avoid any surprise on production environment. It is "S" from SOLID acronym.
2. Open-Close principle (OCP)
Your design ( including classes, methods or functions) should be open for extension by adding new funtionality, but closed for change. It is "O" from SOLID acronym. The goal of this principle is that your code must be tested if you want to add new functionality but should not modify the already existing tested and working code.
3. Liskov Substitution Principle (LSP)
4.Interface Segregation Principle ( ISP)
This principle states that the clients should not be forced to depend on methods they do not use. In order to achieve this, you need break your fat interface into multiple interfaces. In other words, many client specific interfaces are better than one general purpose interface. The "I" in SOLID stands for Interface segregation principle.
5. Dependency Inversion Principle ( DIP)
It is also referred as dependency injection principle. DIP states that the high level modules should not depend upon low level modules. Rather, both should depend upon abstractions. That means, do not ask for dependency, it will be provided to you by framework. Spring framework is totally built using this principle. DIP represents "D" in SOLID acronym.
6. Don't Repeat Yourself ( DRY)
According to this principle, write functionality at one place and only at one place. There should be no copy/paste of code. Instead of writing same code at many places, use abstraction to abstract common things in one place. This principle helps to avoid code scattering and results loose coupling.
7. Program to an interface, not an implementation ( P2I)
If we program to an interface, our code is only dependent on that interface, instead of an implementation. This allows us to plug-in another implementation. Also, this leads to flexible code which can work with any new implementation of interface. This is one of the most important design principle that every programmer should know about.
8. Favor Composition over inheritance
Composition allows to change behavior of a class at run-time by setting property during run-time. Inheritance should be used only when there is "IS-A" relationship, or only when LSP passes. By using Interfaces to compose a class, we can use polymorphism which provides flexibility to replace with better implementation any time.
9. Keep It Simple, Stupid ( KISS)
This principle states that you should try your best to make your design simple to implement, simple to understand, simple to maintain, simple to test. In other word, simplicity should be a key goal in your design and unnecessary complexity should be avoided.
10. Separation of concern ( SoC)
Your design should separate
- Technology from business
- Stable code from changing code
- Business process from application logic
- Implementation from specification
Most of object oriented programming languages such as Java, C#, C++ et. al can separate concerns into objects. Also, architectural design patterns design patterns like MVC can separate content from presentation (view) and the data-processing (model) from content.
More about design principles and design patterns can be found at the references we've listed below. These books helps a lot to write better code, taking full advantage of various Object oriented and SOLID design principles.