Does the method have more than one violation of the principle of single responsibility?

For my purposes, I need to find a specific node in the XML file and, if found, delete it. Should I pull the search functionality into my own method and remove the functionality into my own method? This seems more expensive because I will search the xml file once to see if it exists and look again for it to delete it. If I combine these two functions into one method, I can remove it when I find it. Do I understand SRP correctly here?

+4
source share
4 answers

Do you have other reasons / situations in which you are looking for an xml file? In general, it is a good thing to separate individual tasks at any level, regardless of whether you adhere to or break someone's rule (this is my rule ;-)). Separating these functions may (?) Also make your code more understandable, which may be more important than trivial performance enhancement.

+1
source

Your average XML parser will create nodes that your parents know, so you can do something like:

XmlNode node = this.FindNode(filter); node.ParentNode.DeleteChild(node); 

This way you separated both functions, but not overhead.

Regarding the essence of your Question: Yes, searching and deleting in one method violates a single responsibility, but performance and SRP in many cases do not mix, so you need to decide which is more important.

PS:
An example is not (not in vain) associated with any real language there.

+1
source

No, the principle of single responsibility is not about the details of writing code. This is about how to divide the functionality of the program into classes. It says that if a class can change for several reasons, it should be two classes. A classic example is a class that creates and formats a report; The report content and report format are likely to change at different times, so the class is a good candidate for refactoring by two.

You do not say what the functional responsibility of your class is, but, from the point of view of any work that your class should do, searching and deleting an XML node are only parts of this single task and executing them in one class and in one operation does not violate SRP.

(On the other hand, if your class had a lot of domain logic, as well as a lot of nuts and bolts about XML management, that would violate SRP.)

+1
source

It violates the principle of sharing command requests , which I feel goes hand in hand with SRP. Search and delete are two things that can change, so they can also be defined as two separate responsibilities. They can be tested separately, you may have an error in how you find the node, but not in the deletion business. You can also make fun of the deletion part. It also gives an intermediate point between search and deletion (again, this goes back to unit testing and debugging).

In general, I would say that there are many advantages to separating query requests, so I try to follow it whenever possible.

Do not prematurely optimize your code! Write it down in the way that is most convenient for you / the best design, and then, if it's a bottleneck, you can customize it.

0
source

Source: https://habr.com/ru/post/1303683/


All Articles