Clean Code
Code that is easy to understand, maintain, and modify, following principles of readability, simplicity, and expressiveness.
Also known as: Readable Code, Maintainable Code
Category: Software Development
Tags: software-engineering, code-quality, best-practices, programming, principles
Explanation
Clean code is code that is easy to read, understand, and change. The concept was popularized by Robert C. Martin (Uncle Bob) in his influential book of the same name. Clean code is not about cleverness or brevity - it's about clarity and communication.
## Characteristics of clean code
- **Reads like well-written prose** - Intention is clear without extensive comments
- **Does one thing well** - Each function, class, and module has a single responsibility
- **Minimal dependencies** - Low coupling between components
- **Has tests** - Verified behavior enables confident changes
- **No surprises** - Behaves as the reader expects
- **No duplication** - Every piece of knowledge is expressed in exactly one place
- **Expressive naming** - Variables, functions, and classes reveal their purpose
## Clean code principles
1. **Meaningful names** - Choose names that reveal intent. `elapsedTimeInDays` over `d`
2. **Small functions** - Functions should do one thing, do it well, and do it only
3. **One level of abstraction** - Don't mix high-level policy with low-level detail
4. **Minimal arguments** - Fewer parameters make functions easier to understand and test
5. **No side effects** - Functions should do what their name says, nothing more
6. **Error handling as a separate concern** - Don't obscure logic with error handling
7. **Comments only when necessary** - Prefer self-documenting code; use comments to explain 'why', not 'what'
## The cost of unclean code
- **Slower development** - Developers spend more time reading than writing code
- **More bugs** - Unclear code hides defects
- **Higher onboarding cost** - New team members struggle to understand the system
- **Fear of change** - Nobody wants to touch fragile, confusing code
- **Morale impact** - Working in a messy codebase is demoralizing
## Clean code is a practice, not a destination
Code is never 'finished clean.' It requires ongoing attention through:
- Regular refactoring during development
- Code reviews focused on clarity
- The Boy Scout Rule (leave it cleaner than you found it)
- Team agreements on coding standards
Clean code is ultimately about empathy for the next developer who will read your code - which is often future you.
Related Concepts
← Back to all concepts