Is there a way to apply the Single Responsibility Principle to a function where two things need to occur in a loop in order to not need to iterate twice?
For example, suppose I have a function like the following (in pseudo-Python):
def process_stuff(list):
for item in list:
process_one_way(item)
process_another_way(item)
In this function, the data needs to be processed twice in two completely separate ways (in particularly, an example I recently worked with involved both creating some objects from the data in the list, and then also generating separate JSON for a different purpose). I feel like this violates the SRP because this function is doing two completely separate things with the data. A natural way to separate them would be:
def process_stuff(list):
process_one_way(list)
process_another_way(list)
def process_one_way(list):
for item in list:
# process
def process_another_way(list):
for item in list:
# process
... but that involves iterating through the list twice, which seems wasteful. Is there a better way to separate these two tasks, or am I misunderstanding what the intention of the SRP is?
process_stuff
function does not do only one thing, that is looping over the collection and applying specific transformation to its items?