---
name: null-object
description: This skill should be used when the user asks about "null object pattern", "when to use null object", "avoid null checks", "null reference", "special null case", or mentions eliminating null/undefined checks throughout code.
version: 1.0.0
---

# Null Object Pattern

## What Is It
Null Object provides a surrogate object that implements the expected interface but does nothing. It eliminates the need for null checks by providing a neutral "do nothing" behavior.

## When to Use
- **Frequent null checks** - Same null guard everywhere
- **Polymorphic behavior** - Want to treat null as valid object
- **Default behavior needed** - Safe no-op operations
- **Reduce conditionals** - Eliminate if-null patterns

## When NOT to Use
- **Null means error** - Missing object is exceptional
- **Performance critical** - Object creation overhead
- **Different null states** - Multiple "empty" meanings
- **Simple primitives** - Overkill for basic types

## How to Implement

```typescript
// Define interface
interface Logger {
  log(message: string): void;
}

// Real implementation
class ConsoleLogger implements Logger {
  log(message: string) {
    console.log(message);
  }
}

// Null object - does nothing
class NullLogger implements Logger {
  log(message: string) {
    // Intentionally empty - do nothing
  }
}

// Usage - no null checks needed
function process(logger: Logger) {
  logger.log("Processing..."); // Works with NullLogger too
}

process(new ConsoleLogger()); // Logs
process(new NullLogger());    // Silently does nothing
```

## Code Examples
- `examples/null-object.ts` - Basic implementation
- `examples/null-object-advanced.ts` - Real-world usage

## Related Patterns
- **Strategy** - Null object as a strategy
- **State** - Null as a valid state
- **Command** - Null command pattern
