views:

58

answers:

2

For my purposes, I need to search for a specific node in an xml file and, if found, delete it. Should I pull search functionality out into its own method and delete functionality out into its own method? It seems more expensive to do it this way because I'll be searching the xml file once to see if it exists and searching it again to delete it. If I combine these two functionalities into a single method I can delete it right when I find it. Am I understanding SRP correctly here?

+1  A: 

Your average XML Parser will create Nodes which know their Parents so you can do something like:

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

This way you have split both functions but no overhead.

Regarding the core of your Question: Yes, searching and deleting in one Method violates the single responsibility but performance and SRP don't mix that well in many cases so you have to decide whats more important.

PS:
Example is not (knowingly) related to any real language out there.

dbemerlin
+1  A: 

Do you have any other reasons/situations in which you are searching the xml file? In general it is a Good Thing to separate distinct jobs at any level, regardless of adhering to or violating someone's rule (that's my rule ;-) ). Separating these functions might (?) also make your code more understandable, which may turn out to be more important than a trivial gain in performance.

mickeyf
Problem is, you have definitely violated SRP by doing this, as you have just created another reason for your class to change: 'I need it to work on a 16Mb XML file 5 times per second'.SRP is about changing code that is organised in classes or modules. Any time someone uses it as a metaphor for some situation that doesn't involve a programmer typing stuff, they are misusing it.
soru