Today I am going to start a series of posts about SOLID. SOLID is a group of 5 patterns, each one represented by a letter, starting by S.
But Raphael, what is SOLID?
Each SOLID's letter |
- Decreases your code's maintenance effort
- Improves your code's understanding
- Decreases the coupling between objects
SRP - Single Responsibility Principle
As the name suggests, each class must have the behaviors within their responsability, here is an example of a class outside the SRP:
Class outside the SRP |
In that class, beyond his responsibility, update the name and the address, it send e-mails as well, what is outside his responsibility. In this example I used a small class, but imagine a very large class with more than 1000 lines with many behaviors outside his responsibility like do some calculation, persist on the database, generate one or more reports, and more others behaviors, how hard it would be the maintenance of the code? And the class has a strong coupling with the reference System.Net.Mail, what is very wrong as well.
How to fix it?
To fix it you need to isolate the method SendMail in a class that have as responsibility manipulate e-mails. If exists other behaviors as the cited previously you need to isolate them as well.
Email class |
Client class in SRP |
This is the SOLID's most basic pattern, next week I will write about the next pattern, Open Closed Principle.
No comments:
Post a Comment