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:        # processdef 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?
How to apply SRP in a loop where two things need to be done?
single responsibility;loops
The SRP does not say a function should only do one thing. If that was the case you couldn't write an application or system which does more than one single thing!What you should consider is: Will the two operations always be performed on the same collections at the same time? Then they belong in the same loop. Take this example:def end_of_year_stuff(customers):    for customer in customers:        send_christmas_card(customer)        calculate_for_financial_statement(customer)Here it is kind of by accident these two things happen at the same time. The company might change its financial year without Christmas being moved, or Christmas might be called off without it affecting the financial year. Or you might decide to exclude customers in Agrabah from the Christmas card, but they still have to be considered for the financial reports. In short, the two concerns might change independently and therefore belong in separate loops.Now take this example:   def roll_logs(logfiles):       for logfile in logfiles:          compress_file(logfile)          move_to_backup_drive(logfile)In this case the two operations belong together in the same loop, since you want to make sure these two things happen together.The important thing to realize is SRP depends on the meaning and purpose of the functions and operations involved. So by using semantic-free names like process_stuff and process_one_way in the example, it is impossible to say anything about whether the code violates SRP or not.