1

If a function is being used to process incoming results, I believe an SRP violation exists if it contains all the logic:

def process_response(response):
   '''Process the results of the web request'''

   # logic to parse response
   # logic to format response
   # logic to write response

This function is doing a lot, and its clear. But if the logic was separated out, such that the function composed of calls:

def process_response(response):
    '''Process the results of the web request'''

    parsed = parse_response(response)
    formatted = format_response(parsed)
    save_response(formatted)

Even though the function is calling several functions to perform independent actions, do we say that process_response has a single responsibility? I suppose it is relative to the level of abstraction you give to the current function. Explicitly stated, it is responsible for calling the parser and calling the formatter and calling the saver. When explained as such, it appears that process_response has several responsibilities. However, when stated it's responsible for "managing the processing workflow", it appears as a single responsibility.

I would think it impossible to have no functions that are not composed of calls when developing a system. Therefore, is it safe (or correct) to view a composed function as having a single responsibility of executing its callees?

pstatix
  • 1,017
  • 10
  • 16

1 Answers1

3

The SRP doesn't apply to functions.

Functions are "Do one thing, and do it well." SRP is "Each class should have a single overarching responsibility."

For example, a Customer repository class might have Create, Read, Update and Delete methods. Each method does one thing, and one thing only. The Customer class has one responsibility: database access for customer objects.

Functions are routinely used to combine the results of smaller functions, and you can still call that "doing one thing" with proper naming, but that's not SRP. SRP applies to classes, not functions.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • Side note: the "Do one thing, and do it well." was originally known as the Unix philosophy. AFAIK it has become a mantra used for several things, which includes also the SRP. And yes, Bob Martin used it also for describring the quality of functions, – Doc Brown May 18 '21 at 15:36