{
    "slug": "single_responsibility",
    "term": "Single Responsibility Principle",
    "category": "general",
    "difficulty": "beginner",
    "short": "A class or function should have one reason to change — doing one thing and doing it well.",
    "long": "The S in SOLID, SRP states that every module, class, or function should be responsible for one part of the system's functionality. \"One reason to change\" means: if two different types of requirements changes would require editing the same class, that class probably has too many responsibilities. SRP makes code easier to understand, test, and modify because changes to one concern don't accidentally break another.",
    "aliases": [
        "SRP",
        "Single Responsibility Principle",
        "one reason to change"
    ],
    "tags": [
        "general",
        "solid",
        "oop",
        "principles"
    ],
    "misconception": "SRP means a class should only do one thing. SRP means a class should have only one reason to change — one stakeholder or concern driving its evolution. A class with two unrelated responsibilities will be modified for two different reasons, coupling changes that should be independent.",
    "why_it_matters": "Classes with a single responsibility are easier to test in isolation, safer to change without side effects, and simpler to reuse. Most spaghetti codebases trace back to classes that grew to own too many concerns.",
    "common_mistakes": [
        "Confusing SRP with \"one method per class\" — it means one reason to change, not one action.",
        "Putting database queries, business logic, and HTTP response building all inside a controller.",
        "Splitting too aggressively — a 5-line helper class for every trivial operation creates its own maintenance burden.",
        "Ignoring SRP during prototyping and never refactoring once the code \"works\"."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "god_class",
        "code_smell"
    ],
    "prerequisites": [
        "solid",
        "extract_function",
        "cohesion"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Single-responsibility_principle"
    ],
    "bad_code": "class User {\n    public function save(): void { /* DB logic */ }\n    public function sendWelcomeEmail(): void { /* SMTP logic */ }\n    public function toJson(): string { /* serialisation */ }\n    public function validatePassword(string $pw): bool { /* validation */ }\n    // Four reasons to change\n}",
    "good_code": "class OrderController {\n    public function store(Request $r, OrderService $orders) {\n        $order = $orders->create($r->validated());\n        return response()->json($order);\n    }\n}\n// OrderService handles business logic, dispatches events/jobs",
    "quick_fix": "If you struggle to name a class in one word (not 'Manager', 'Helper', 'Util'), it has multiple responsibilities — split it",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-13",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/single_responsibility",
        "html_url": "https://codeclaritylab.com/glossary/single_responsibility",
        "json_url": "https://codeclaritylab.com/glossary/single_responsibility.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Single Responsibility Principle](https://codeclaritylab.com/glossary/single_responsibility) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/single_responsibility"
            }
        }
    }
}