iTranslated by AI
Stop Multi-layered Function Subcontracting: SRP and the Concept of Function Responsibility
"Multi-tier subcontracting structures are bad"—this is an idea that has become quite well-known. However, programmers often write multi-tier subcontracting code without even realizing it. In fact, many of you might be doing it by accident.
Naturally, multi-tier subcontracting is also bad within a codebase. In this article, I will explain what multi-tier subcontracting code is, its problems, and how to avoid it.
What is code with a multi-tier subcontracting structure?
Code with a multi-tier subcontracting structure is code that passes tasks around from one function to another.
For example, let's consider a scenario for placing an order on an e-commerce site. The server implementation looks something like this:
def 注文API():
注文処理(price)
集計DBにログを送る()
def 注文処理(price):
決済する(price)
履歴に保存する()
def 決済する(price):
if キャンペーン期間中だったら:
支払う(price * 0.8)
else:
支払う(price)
注文完了メールを送る()
At first glance, it doesn't look bad at all. Many people have probably written similar code. However, this code suffers from the problem of multi-tier subcontracting. For example, the payment processing task is being passed around multiple times (注文API → 注文処理 → 決済する → 支払う).

The assigned task is simply thrown to the function below, falling into multi-tier subcontracting.
Multi-tier subcontracting leads to various problems.
- It's unclear which function is responsible for what
- Naming functions properly becomes difficult
- It makes refactoring harder
Problem 1: It's unclear which function is responsible for what
For example, try to figure out from the code "at what point is the order completion email sent?" Because the email sending task is being passed around, you probably won't even have a clue where it is without reading the entire codebase. From the start, the 注文API should have assigned the task directly to the 注文完了メールを送る() function.
Reading through all the code without a clue is very tough. Investigating bugs in a situation where you have to re-read everything every time is a terrible experience. It's bad DX (Developer Experience).
Digression: What is a function's "responsibility" in the first place?
So far, I've been using the word "responsibility" loosely, but what does it actually mean?
I believe that "executing the assigned task without bugs" is the responsibility that a function should fulfill.
As the number of assigned tasks (responsibilities) increases, the function becomes more complex, prone to bugs, and harder to overlook. Therefore, a function's responsibility should be narrowed down to just one.
Problem 2: Naming functions properly is difficult
In multi-tier subcontracting, because there are so many tasks involved, it's difficult to give functions appropriate names. For example, in the previous case, the 注文処理 (OrderProcessing) function was assigned multiple tasks (payment, sending emails, saving history). As a result, there was no choice but to give it an ambiguous name like 注文処理.
When names are ambiguous, it becomes difficult to decipher the code. For example, to find where the responsibility for the payment task lies, you have to read all the code details. Because you have to read every detail, multi-tier subcontracting is, frankly, terrible.
Problem 3: It makes refactoring harder
Beyond code readability, multi-tier subcontracting also impacts refactoring. This is because you end up modifying a function that handles multiple tasks, meaning you must consider the impact on other features when you refactor. It also blatantly violates the Single Responsibility Principle (there should be only one reason to change a function).
Furthermore, it's difficult to write tests in a multi-tier subcontracting structure. Since testability strongly affects the ease of refactoring, it makes refactoring even more difficult.
What to do: Narrow down the tasks assigned to a function to just one
The cause of all these problems is assigning multiple tasks to a single function. In other words, it's a violation of the Single Responsibility Principle.
For example, the 決済する (Payment) function is responsible for two features—payment and sending a completion email—violating the Single Responsibility Principle. The 注文処理 (OrderProcessing) function is even worse, bearing the responsibility for three features when you include the history saving function.
To prevent this, you should split the tasks. Let's try it. First, let's organize the tasks the server is performing:
- Give 20% off all products if it's during the campaign period
- Save order information to history
- Send an order completion email
- Send logs for aggregation
Based on this, let's revise the API.
def 良い注文API():
if キャンペーン期間中だったら: # If during campaign period
支払う(price * 0.8) # Pay
else:
支払う(price)
履歴に保存する() # Save to history
注文完了メールを送る() # Send order completion email
集計DBにログを送る() # Send logs to aggregation DB

The passing around of tasks has disappeared, and the delegation relationship has become flat.
While what the API is doing hasn't changed, it has become much simpler and clearer. In the revised code, the role each function plays is obvious. Additionally, it's easy to understand what the API as a whole is doing.
Note: This isn't about just making everything flat
One thing to be careful about is that I'm not saying you should just make everything flat. If you are splitting tasks into logical units, it's perfectly fine to have further splitting in the lower layers.
For example, in the 注文API (Order API) discussion above, I simplified it, but in reality, there would likely be things like a 履歴API (History API) or a 顧客API (Customer API). Task splitting would also be occurring within those APIs.

There are several APIs within the server, and tasks are further split within each of those APIs.
The problem is passing an assigned task down to the next level without splitting it into meaningful units.
Be mindful of groupings when splitting tasks
Best practices for task splitting are already established. That is cohesion. Cohesion is an indicator of how well things are grouped together. Good grouping is called "high cohesion." Consciously aiming for high cohesion in functions often leads to better code.
The following three types have particularly high cohesion:
- Functions that group operations on the same data (Communicational Cohesion)
- Functions where the output of one process becomes the input for another (Sequential Cohesion)
- Functions that perform a single task (Functional Cohesion)
In our sample code, functions like 履歴に保存する (SaveToHistory) and 注文完了メールを送る (SendOrderCompletionEmail) have high cohesion (functional cohesion) and are well-grouped. Conversely, the 注文処理 (OrderProcessing) function has low cohesion because it mixes multiple tasks.
Generally, multi-tier subcontracting is a state of low cohesion. Create functions that form meaningful groups to increase cohesion and stop multi-tier subcontracting.
(I will leave the detailed explanation of cohesion to other articles. There are many excellent articles out there, so please refer to them.)
Summary: Create functions with high cohesion and stop multi-tier subcontracting
As we have seen, assigning multiple separate tasks to a single function is a bad practice. It tends to increase the cognitive load when reading the code and makes refactoring more difficult.
To avoid this, organize and split tasks, and create functions in a way that ensures high cohesion.
It is easy to fall into multi-tier subcontracting if you are not careful (especially during later modifications). Since improvement doesn't happen overnight, it is important to stay mindful of task splitting and delegation in your daily development.
Discussion