# Craft Null Object

> Crafting Null Objects. Safe defaults instead of null checks.

- Skill: `attac-t/craft-null-object` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add attac-t/craft-null-object`
- Raw SKILL.md: https://api.skillmd.com/api/skills/attac-t/craft-null-object/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: attac-t (https://skillmd.com/u/attac-t)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/attac-t/craft-null-object

---


# Skill: Craft Null Object

> "Instead of checking for null everywhere, return an object that behaves safely."

## The Standard

1. **Null Object behaves like real object**: Same interface, safe defaults.
2. **`withDefault()` for relationships**: Laravel's built-in null object pattern.
3. **Custom Null classes for complex behavior**: When defaults need methods.

## The Anti-Patterns

| ❌ Don't                           | ✅ Do                            | Why                           |
|-----------------------------------|----------------------------------|-------------------------------|
| `$user ? $user->name : 'Guest'`   | `$user->name` with null object   | Eliminate null checks         |
| Null checks in every template     | `withDefault()` on relationship  | Single source of truth        |
| `optional()` for everything       | Null object for repeated access  | `optional()` is for one-offs  |

## Real-World Examples

See [examples.md](examples.md).

