Clean Code
Code written for humans first, machines second.
Code is read far more often than it is written. Clean code means names that reveal intent, small functions with a single job, and structure you can navigate without thinking too hard. The goal is to be able to make self-explanatory code that can be easily navigated while reducing the confusion tax for new contributors and future you. Code that makes you proud to ship it, and to maintain it.
Key principles
- Names should reveal intent — if you need a comment to explain a variable, rename it
- Use SOLID principles to keep code modular and decoupled, but avoid over-engineering
- Functions should do one thing, at one level of abstraction
- Prefer pure functions and immutability where the domain allows
- Kill dead code on sight — version control remembers it
- Consistency beats cleverness — one idiom used everywhere beats three used once each
Recommended resources
- bookClean Code: A Handbook of Agile Software Craftsmanship
The great starting point. Read critically — some advice is dated, the principles are not.
- bookClean Architecture: A Craftsman's Guide to Software Structure and Design
The architectural patterns that emerge when clean code principles are applied at scale.
- bookPatterns of Enterprise Application Architecture
The catalog of common patterns in enterprise software, many of which are anti-patterns in smaller codebases.
- bookRefactoring
The catalog of safe, mechanical code transformations.
Common pitfalls
- Reading about it without doing it — refactor your own code as you learn
- Over-engineering before it is necessary — YAGNI is a real principle
- Over-commenting code that should just be renamed, or saying obvious things
- Letting code rot — if it isn't clean, fix it or delete it