![]() The rules are not clear to where we should draw the line, so we can potentially find different „right ways“ to implement the same feature.īut still, the bottom line is that no matter what someone thinks about what reason to change is, we should all strive to write readable and maintainable code thus implementing Single Responsibility Principle in our own way. Everyone interprets this subjectively or rather how he/she feels it should be implemented. There is no strict rule which states what is that „one reason to change“ in our class. It means that different methods are joined to do one thing and to do it well.įinally, our classes are less dependent on each other (decoupled) which is one of the most important things to achieve while working on a project. When we implement SRP in our code, our methods become highly related (coherent). Furthermore, with such a code, testing becomes easier as well. As we reduce code complexity, our code becomes readable and therefore maintainable.Īs we could see from our example, if our class does its job well, we can reuse its logic in a project. Because we are trying to accomplish only one task in our class, they have become free of clutter and simple to read. The first one being that it has become less complex. Our code has improved in several ways by implementing SRP. ![]() Benefits of Single Responsibility Principle Now every class we have is responsible for one thing and one thing only. ![]() Var saver = new "WorkReport.txt", "Schedule.txt", scheduler) Īfter we execute this code, we will have our file saved in a required location on a defined schedule. String.Join(Environment.NewLine, _entries.Select(x => $"Code: ) Public void RemoveEntryAt(int index) => _entries.RemoveAt(index) Public void AddEntry(WorkReportEntry entry) => _entries.Add(entry) The next step is creating a WorkReport class which will handle all the required features for our project: public class WorkReport So we are going to start with a simple model class: public class WorkReportEntry Imagine if we have a task to create a work report feature that, once created, can be saved to a file and perhaps uploaded to the cloud or used for some other purpose.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |