{
    "slug": "premature_optimisation",
    "term": "Premature Optimisation",
    "category": "quality",
    "difficulty": "beginner",
    "short": "Optimising code before measuring where the actual bottleneck is — trading readability for performance gains that often don't matter.",
    "long": "Donald Knuth's famous observation — 'premature optimisation is the root of all evil' — warns against sacrificing code clarity for performance improvements that are speculative or negligible. Most performance problems lie in a small fraction of the code; optimising the wrong parts wastes time and produces harder-to-maintain code. The correct approach: write clear, correct code first; measure with a profiler to find real bottlenecks; optimise only what the data confirms is slow. In PHP, micro-optimisations (avoiding function calls, hand-rolling loops) almost never matter — I/O and query patterns almost always do.",
    "aliases": [
        "early optimisation",
        "premature optimization",
        "Knuth rule"
    ],
    "tags": [
        "performance",
        "principles",
        "quality"
    ],
    "misconception": "Premature optimisation means you should never think about performance until the end. It means not optimising before you have measured — designing for reasonable algorithmic complexity from the start is not premature optimisation, it is good engineering.",
    "why_it_matters": "Optimising before profiling makes code harder to read and maintain in exchange for performance improvements that are often unmeasurable — the bottleneck is almost never where developers intuit it is.",
    "common_mistakes": [
        "Micro-optimising loop constructs (for vs foreach vs while) without benchmarking actual impact.",
        "Using static methods or avoiding OOP for 'performance' in application code where the overhead is negligible.",
        "Caching results before measuring whether the operation is actually slow.",
        "Choosing a complex algorithm over a simple one for scale that the application will never reach."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "profiling",
        "yagni",
        "technical_debt",
        "code_smell"
    ],
    "prerequisites": [
        "profiling",
        "big_o_notation",
        "refactoring"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Program_optimization#When_to_optimize"
    ],
    "bad_code": "// Micro-optimised before profiling — complex and wrong\npublic function getActiveUsers(): array {\n    // Avoid array_filter — 'too slow'\n    \\$result = [];\n    \\$users  = \\$this->users;\n    \\$count  = count(\\$users); // cache count 'for speed'\n    for (\\$i = 0; \\$i < \\$count; \\$i++) {\n        if (\\$users[\\$i]->active) \\$result[] = \\$users[\\$i];\n    }\n    return \\$result;\n}",
    "good_code": "// Write the clear version first — profile before optimising\npublic function getActiveUsers(): array {\n    return array_values(array_filter(\\$this->users, fn(\\$u) => \\$u->active));\n}\n\n// If this is actually slow (profiler shows it) — fix the query, not the PHP:\n// SELECT * FROM users WHERE active = 1  (let the DB filter, not PHP)",
    "example_note": "\"Premature optimisation is the root of all evil\" — Knuth. Profile first, then optimise the measured bottleneck.",
    "quick_fix": "Write readable correct code first; profile to find actual bottlenecks; optimise only the hot paths that profiling identifies — the 3% that matters",
    "severity": "info",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/premature_optimisation",
        "html_url": "https://codeclaritylab.com/glossary/premature_optimisation",
        "json_url": "https://codeclaritylab.com/glossary/premature_optimisation.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": "[Premature Optimisation](https://codeclaritylab.com/glossary/premature_optimisation) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/premature_optimisation"
            }
        }
    }
}