These 5 principles will guide you on how to write better code. Though they come from object-oriented programming.
It’s much easier to work on small Singly Responsible parts whose changes don’t affect any upstream or downstream part.
S - Single responsibility principle
A class should have only one reason to change. It means that a class should have only one responsibility or purpose
O - Open closed principle
Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.
L - Liskov substitution principle
Subtypes must be substitutable for their base types.
I - Interface segregation principle
Clients should not be forced to depend on interfaces they do not use.
D - Dependency Inversion principle
High-level modules should not depend on low-level modules; both should depend on abstractions.
Helping the code to:
Tolerate change.
Ease code understanding.
Write components that can be used in many software systems.
NOTE
SOLID is a must know skill in OO development but I think we should be willing to modernize it and make it fit in 2022 when industry is moving towards functional coding. I’m a 20 year c# vet and do full solid on most projects but it’s hard to justify the abstractions for smaller projects. You end up with too many single method classes which can be functions instead.
S — Single responsibility principle (SRP)
“There should never be more than one reason for a class to change.” same idea different words: “a class should have only one job and do one thing”
classBookPersistence { publicsaveToFile(book: Book): void { // some fs.write method to save book to file } }
O — Open-Close Principle (OCP)
“Software entities should be open for extension, but closed for modification.”
There are two primary attributes in the OCP:
It is open for extension — This means you can extend what the module can do.
It is closed for modification — This means you cannot change the source code, even though you can extend the behavior of a module or entity.
OCP means that a class, module, function, and other entities can extend their behavior without modifying their source code. In other words, an entity should be extendable without modifying the entity itself.
// Bad // If we add another shape later, which means we need to create a new class. // In that case, we would also need to modify the AreaCalculator class to calculate the area of the new class. That’s against the Open-Close Principle.
If you have a function that works for a base type, it should work for a derived type.
In simple terms, if a program is written to work with a certain type (superclass), it should also work correctly with any of its derived types (subclasses) without requiring any modifications.
// A classic example of a violation of this principle is the Rectangle-Square problem. // The Square class extends the Rectangle class and assumes that the width and height are equal. When calculating the area of a square, we’d get a wrong value.
publicarea() : number { returnMath.pow(this._height, 2); } }
I — Interface segregation principle (ISP)
“Many client-specific interfaces are better than one general-purpose interface.”
The principle suggests that using client-specific interfaces instead of a general-purpose interface prevents the implementation of unnecessary methods in classes, ensuring better design.
“Entities must depend on abstractions not on concretions. It states that the high level module must not depend on the low level module, but they should depend on abstractions.”
This principle states that a class should not depend on another class, but instead on an abstraction of that class. It allows loose-coupling and more reusability.
It means that classes should depend on abstractions (interfaces or abstract classes) rather than concrete implementations.
Design Principles and Design Patterns:
“If the open-closed principle (OCP) states the goal of object oriented (OO) architecture, the DIP states the primary mechanism”.
// Bad // Here, the Post class depends on the MemoryStorage class to save new posts. // What happens if we need to change the storage used to save posts? We’ll have to modify the PostService class to change the type of the db property, thus violating the Open-Closed Principle .