Single responsibility principle
Принцип единственной ответственности
A class or module should have one reason to change. Keep code together when it changes in response to the same requirements, and separate code that changes for independent reasons.
Also known as: SRP
What does “one responsibility” mean?
The single responsibility principle (SRP) is the first of the five SOLID design principles. It says that a module should have only one reason to change. The useful question is not “How many things does this code do?” but “What kinds of requests would make us edit it?”
Consider a report that includes customer names. One policy determines which names may appear; another determines how columns are displayed. A change to the column order should not require editing the privacy policy. Several methods can still belong together if they implement the same policy, and a single short method can violate SRP if it implements two unrelated policies.
How to spot a responsibility boundary
Start with real change requests. For each one, identify the rule that changes, who or what drives it, and the code you would edit. If a change requested by the privacy team and a layout request from the reporting team both send you to the same implementation, look for a way to separate those decisions. Team names are clues, not a rule: one team can own several independently changing policies.
A useful boundary lets you change and test one policy without needing to understand the implementation of the other. On the other hand, if every report layout change requires matching edits in three new classes, the split may have scattered one responsibility instead of isolating it.
Example: generating a customer report
Suppose build_report() decides which customer fields can be disclosed and produces a CSV file. Pull the disclosure rules out of the CSV formatting code:
def build_report(customers, audience):
rows = select_disclosable_fields(customers, audience)
return render_csv(rows)
The first function decides what the audience may see. The second converts the approved data to CSV and does not need to know who will read it. You can test that an email address is withheld without parsing CSV, and test CSV quoting with sample data that does not involve access rules. The wrapper still has a responsibility of its own: coordinating the report workflow.
Where it helps and where to stop
SRP applies to functions, classes, and modules. In a web app, for example, separating request parsing from pricing rules lets you test discounts without starting a web server. That separation can live entirely within one application; it does not call for a microservice or an interface for every function. Keep operations together when they enforce the same rule, especially if pulling them apart makes that rule harder to follow.
How it comes up in interviews
You may be asked to refactor a class that does several things. Rather than counting methods, name two plausible changes and show where each would go after the refactor. In the report example, a new disclosure rule belongs in field selection, while a change to CSV quoting belongs in rendering. Explain how you would test each independently and what the coordinating function would still need to do.