{
    "slug": "null_object",
    "term": "Null Object Pattern",
    "category": "quality",
    "difficulty": "intermediate",
    "short": "Replace null with an object that implements the expected interface but performs no operation, eliminating null checks throughout the codebase.",
    "long": "The Null Object pattern (Bobby Woolf) provides a do-nothing implementation of an interface that can be used wherever a null check would otherwise be required. For example, a NullLogger that implements LoggerInterface but discards all messages replaces if ($logger !== null) $logger->log(...) checks everywhere. The Null Object makes the absence of something explicit and type-safe, simplifies calling code, and follows Tell Don't Ask. PHP 8+ Null Object are often combined with union types and nullsafe operators as complementary approaches.",
    "aliases": [
        "null object pattern",
        "NullObject",
        "do-nothing object"
    ],
    "tags": [
        "design-pattern",
        "oop",
        "null-safety"
    ],
    "misconception": "Null object pattern is just a fancy way to avoid null checks. It eliminates entire branches of conditional logic by providing a safe default behaviour — code that calls methods on a NullObject works without branching, making it structurally simpler.",
    "why_it_matters": "A null object implements the same interface as a real object but does nothing — it eliminates null checks at call sites and makes the absence of a thing an explicit, safe concept.",
    "common_mistakes": [
        "Not implementing the full interface — callers that use methods not on the null object still get null errors.",
        "Null objects that return null from methods — they should return safe defaults (empty string, 0, empty array).",
        "Using null objects where an Optional/Maybe type better communicates the possible absence.",
        "Applying null objects everywhere — sometimes null carries meaning that should be handled, not silently swallowed."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "null_coalescing",
        "nullsafe_operator",
        "defensive_programming",
        "interfaces"
    ],
    "prerequisites": [
        "null_coalescing",
        "interfaces",
        "defensive_programming"
    ],
    "refs": [
        "https://refactoring.guru/introduce-null-object"
    ],
    "bad_code": "// Null checks spread throughout call sites\n$discount = $user->getDiscount();\n$price = $discount !== null ? $price * (1 - $discount->rate()) : $price;",
    "good_code": "interface Discount {\n    public function apply(float $price): float;\n}\n\nclass PercentDiscount implements Discount {\n    public function __construct(private float $rate) {}\n    public function apply(float $price): float { return $price * (1 - $this->rate); }\n}\n\nclass NullDiscount implements Discount {\n    public function apply(float $price): float { return $price; } // no-op\n}\n\n// Now: no null check needed at call sites\n$price = $user->getDiscount()->apply($price);",
    "quick_fix": "Create a NullLogger or GuestUser that implements the same interface as the real object but does nothing — callers never need to check for null",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/null_object",
        "html_url": "https://codeclaritylab.com/glossary/null_object",
        "json_url": "https://codeclaritylab.com/glossary/null_object.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": "[Null Object Pattern](https://codeclaritylab.com/glossary/null_object) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/null_object"
            }
        }
    }
}