{
    "slug": "lines_of_code",
    "term": "Lines of Code (LOC) as a Metric",
    "category": "quality",
    "difficulty": "beginner",
    "short": "The simplest size metric — useful for normalising other metrics and tracking growth, but a poor quality indicator when used in isolation.",
    "long": "Lines of Code has three variants: physical LOC (all lines), source LOC (non-blank, non-comment), and logical LOC (statements). LOC is useful for normalising defect density (defects per KLOC) and tracking codebase growth trends. As a standalone quality metric it is misleading — a 500-line function and ten 50-line functions share the same LOC but differ enormously in maintainability. LOC is best used alongside complexity metrics (cyclomatic, cognitive) rather than as a target. Setting LOC limits per file in CI is a lightweight smell detector that flags candidates for refactoring review.",
    "aliases": [
        "LOC",
        "SLOC",
        "source lines of code"
    ],
    "tags": [
        "metrics",
        "quality",
        "productivity"
    ],
    "misconception": "Fewer lines of code always means better code. Extremely compact code can be harder to read and maintain than slightly more verbose but explicit code. LOC is a weak proxy for complexity — use it alongside other metrics, never alone.",
    "why_it_matters": "Lines of code is the simplest size metric — while not a quality metric by itself, abnormal counts (very high or very low) reliably indicate areas worth examining for complexity or laziness.",
    "common_mistakes": [
        "Treating LoC as a productivity metric — incentivising more lines produces bloated, verbose code.",
        "Using LoC as the only size metric — a 50-line function with 15 branches is more problematic than a 200-line class with simple sequential logic.",
        "Reducing LoC by compressing multiple statements onto one line — not an improvement.",
        "Not normalising LoC by language — PHP and Java will naturally produce different counts for equivalent logic."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "cyclomatic_complexity",
        "cognitive_complexity",
        "maintainability_index",
        "method_length"
    ],
    "prerequisites": [
        "cognitive_complexity",
        "halstead_index",
        "cyclomatic_complexity"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Source_lines_of_code"
    ],
    "bad_code": "// 'Reducing' LoC by unreadable compression — worse, not better:\nfunction p($u){return$u->a&&$u->v&&!$u->b?'active':($u->b?'banned':'inactive');}\n// Low line count, zero readability — meaningful LoC reduction requires refactoring, not compression",
    "good_code": "// Lines of Code (LOC) is a simple but blunt metric\n// Logical LOC (non-blank, non-comment) is more meaningful\n\n// Tools to measure PHP LOC:\n$ phploc src/\n// Reports: LOC, LLOC (logical), CLOC (comment), NCLOC, methods, classes\n\n// Rough guidelines (not strict rules):\n// Method: < 20 LLOC\n// Class:  < 200 LLOC\n// File:   < 400 LLOC\n\n// LOC alone doesn't indicate quality — pair with:\n// - Cyclomatic complexity\n// - Test coverage\n// - Maintainability Index\n\n// SonarQube and PHPMetrics produce LOC dashboards:\n$ phpmetrics --report-html=report/ src/",
    "quick_fix": "Use LOC as a starting indicator only — high LOC signals classes worth examining, but quality is better measured by cyclomatic complexity, cohesion, and test coverage",
    "severity": "info",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/lines_of_code",
        "html_url": "https://codeclaritylab.com/glossary/lines_of_code",
        "json_url": "https://codeclaritylab.com/glossary/lines_of_code.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": "[Lines of Code (LOC) as a Metric](https://codeclaritylab.com/glossary/lines_of_code) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/lines_of_code"
            }
        }
    }
}