Before delving into SOLID design principles let us list out characteristics of bad design
- Rigidity -Making changes are difficult as to change one class requires us to modify lots of other classes.
- Fragility – One change breaks many parts of system.
- Immobility – It is hard to reuse classes as it is highly tangled with other part of system.
S.O.L.I.D Design Principle
SOLID stands for SRP, OCP, LSP, ISP and DIP
- Single Responsibility Principle (SRP) – A class should have only one reason to change. Each class should have only one responsibility and hence only one reason to change. If it has more than one reason to change then it should be split into two classes. The reason behind this is that if class with more than one responsibility needs to change then it will affect a larger part of system.
- Open Close Principle (OCP) – A class should be open to extension but close for modification. When ever you want to change behavior of your classes, library or packages etc then instead of modifying them just extend them. This ensures backward compatibility.
- Liskov Substitution Principle (LSP) – Substitutibility is a principle of Object Oriented design. This means that is S is a subtype of T then any object of type T can be replaced by objects of type S without any alteration of code. This is basis of Liksov Substitution Principle. In other words a Derived class should be written in such a way that it should be able to be used instead of Base class without changing the code.
- Interface segregation Principle (ISP) – ISP states that no client should be forced to use an interface that is has no use of. This principle helps us in designing our interfaces. So we should add only those methods in an interface which has to be used by client. ISP tells us to break a large interface into smaller one so that client has to worry about only those methods which it will use. This keeps the code clean, decoupled and easily maintainable.
- Dependency Inversion Principle (DIP) – A high level module should not depend upon low level modules. Both should depend upon abstractions and abstraction should not depend on details, details should depend on abstraction. DIP states that high level module should be decoupled from low level modules by creating a layer of abstraction. Layer of abstraction is a wrapper on low level modules. This layer is created by taking into account the needs of high level modules. Thus abstraction(idea) drives the details and not vice-versa.
These are the famous SOLID design principles. Apart from SOLID there are few other notable designs which are mentions now and then.
YAGNI (You aren’t gonna need it) – This is not a object oriented design per se. This is rather a agile principle for software implementation. You should write only that amount of code what you need. If you write code thinking about your future need then you will end up with bloated, unusable code. And who knows how the design will change in future.
DRY (Don’t repeat yourself) – This is another neat design principle which says that do not write duplicate code. So if you see yourself writing duplicate code then wrap that code in a class (or a method) and start reusing it.
KISS(Keep it simple,stupid) – This is more generic and not a OOP design. This to me is a simple way of rephrasing SRP principle. Your classes should be simple and should do only what it is suppose to do. If it is complicated then break it into multiple classes unless you achieve simplicity.
That is all folks. Do remember to follow these guidelines while designing your class. This will often make you a coder. As it is understood, all the comments, negative or positive are most welcome.
And all the spammers STFU.