oopsingle-responsibility-principlesolid-principles

Does having a method do more than one thing violate the Single Responsibility Principle?


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?


Solution

  • 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.