Clean Code
Meaningful names, small functions, DRY, YAGNI, KISS; code smells and refactoring
Clean Code, popularized by Robert C. Martin's 2008 book, is a discipline of writing code that is readable, minimal, and self-documenting. The core thesis is that code is read far more often than written, so optimizing for reader comprehension pays compound dividends. Practices include meaningful naming, small functions (ideally <20 lines), and elimination of duplication via DRY (Don't Repeat Yourself). Complementary heuristics — YAGNI (You Aren't Gonna Need It) and KISS (Keep It Simple, Stupid) — prevent speculative over-engineering.
Key Points
- Meaningful names: variables, functions, and classes should reveal intent — prefer `getUserAccountBalance()` over `getVal()`.
- Small functions: each function does one thing; if it needs a comment to explain what it does, it should be extracted.
- DRY: every piece of knowledge must have a single, authoritative representation — duplication is the root cause of maintenance bugs.
- YAGNI: do not implement features until they are actually required; unused abstractions add cognitive load with zero delivered value.
- KISS: the simplest solution that works is preferred — complex clever code is a liability, not an asset.
- Code smells to eliminate: long method, feature envy, data clumps, primitive obsession, divergent change, shotgun surgery.
- The Boy Scout Rule: always leave code cleaner than you found it — incremental improvement prevents entropy.
- Comments should explain "why," not "what" — if you need a "what" comment, the code is not clean enough.
Real-World Example
Google's internal style guides enforce clean code at scale: mandatory code reviews, linter-enforced naming conventions, and maximum function complexity metrics via Cyclomatic Complexity checks in their Critique tool.