Excessive nil pointer checks in Go
- Programming
- Developer Tools
- Static Analysis
- Go
- Type Systems
The post argues that a lot of Go nil checking is cargo-cult defensive programming. Its core claim is simple: if a function requires a dependency to work, passing nil is already a bug, so checking for nil deep inside business logic just spreads uncertainty through the codebase. The cleaner move is to reject invalid state where it enters the system, keep the rest of the code operating on valid assumptions, and panic or fail loudly when an internal invariant is broken instead of silently limping on.
Treat repeated nil checks as a design smell, not extra safety. If your codebase depends on invariants like "this dependency always exists," enforce them at construction or module boundaries and back that up with static analysis, not scattered defensive branches.
- konradreiche.com
- Discuss on HN