SOLID: Five Principles of Clean Code
The Objective
Robert C. Martin, a.k.a. Uncle Bob, proposed five principles of software development intended to produce code that is easier to maintain and extend. Collectively, those principles are known by the acronym, SOLID. Here they are:
- S - Single-responsibility Principle
- O - Open-closed Principle
- L - Liskov Substitution Principle
- I - Interface Segregation Principle
- D - Dependency Inversion Principle
a great mnemonic, but what are those principles? Let's go through them here.
Single-responsibility Principle
The Single-responsibility Principle (S.R.P) tells us that a class should have one, and only one clearly defined job. The effect is that changes to only one part of the software spec should affect the specification of the class.
Open-closed Principle
The text-book definition of the open-closed principle is that Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. Said another way, classes, modules and functions should be easily extendable without having to modify the class, module, or function itself.
Liskov Substitution Principle
This principle says that you must be able to substitute sub-types for their base types. What does that mean? Basically, it's saying that every sub- or derived-class should be substitute-able for it's base or parent class. The Liskov Substitution Principle is related the Design by Contract (DbC) approach to software design.
Interface Segregation Principle
The Interface Segregation Principle (ISP) say that clients should not have to implement an interface that it does not use. Interfaces belong to clients, not to libraries or hierarchies.
Dependency Inversion Principle
The Dependency Inversion Principle says 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 upon abstractions.