#📌paradigm
The concept of Clean Code is most often attributed to [the book of the same name by Robert Martin (aka Uncle Bob)](https://www.goodreads.com/book/show/3735293-clean-code). Written in 2008, it became an iconic book for several generations of developers across all domains.
The profound impact of this book can be explained by its pioneering effort to articulate the fundamental principles (such as SOLID, REST) and priorities that have persisted from the earliest days of software craftsmanship. Although some examples may be considered irrelevant to contemporary development practices, the root vision of what is good and evil will probably remain unchanged for many years to come.
Knowing and adopting these principles into your daily work fosters a sense of common ground with many other developers and companies. Lately, some of these principles have been recognized as part of the [[dev excellence|Dev Excellence]] guidelines.
The main goal of the Clean Code concepts is to address code [[maintainability]] and to keep the cost per change under control during the entire project lifecycle.
![[cost of changes.jpeg|600]]
---
## The main ideas
### Meaningful Names
Use descriptive names for variables, functions, classes, and other elements of code. Names should reveal intent and make the code self-explanatory.
### Functions
Keep functions small and focused on doing one thing well. They should have a clear purpose and follow the Single Responsibility Principle (SRP). Functions should also have descriptive names and minimal side effects.
### Comments
Write clean code that is self-explanatory. While comments can be helpful, strive to make the code so clear that comments are seldom needed. Comments should focus on why the code is doing something, not what it is doing (which should be evident from the code itself).
### Formatting
Consistently format code to improve readability. Follow a consistent style guide, and consider using automated tools to enforce formatting rules.
### Error Handling
Handle errors gracefully and don't ignore them. Use exceptions rather than returning error codes, and handle errors as close to where they occur as possible.
### Unit Testing
Write unit tests to ensure that code behaves as expected. Test-driven development (TDD) is encouraged, where tests are written before the code that satisfies them.
### Refactoring
Regularly refactor code to improve its design without changing its behavior. Refactoring helps keep code clean and maintainable over time.
### Dev Principles
#### SOLID
A subset of principles to deliver a code easy to change.
**Refers to:**
- Single Responsibility
- Open-Closed
- Liskov Substitution
- Interface Segregation
- Dependency Inversion
#### KISS
(Keep it Simple, ~~Stupid~~)
Simplicity is key. Avoid unnecessary complexity and strive for the simplest solution that meets requirements.
#### DRY
(Don't Repeat Yourself)
Avoid duplicating code by extracting common functionality into reusable components.
#### Continuous Improvement
Clean code is a journey, not a destination. Continuously strive to improve the quality of your code through learning and practice.
---
## Wrapping up
I would describe the main benefit with a following storyline:
Most developers learn programming languages and frameworks first on ideal-world examples.
Feeling empowered, they apply them to real-life projects, facing real-life user scenarios, domain and infra challenges, and making their own decisions to mitigate them.
Without guidance, they usually get trapped into a dead end of unmaintainable buggy code and failed projects.
The talented will handle them once and gain experience to be re-used later.
The lucky ones will have a mentor to ask to share their own previously gained experience.
But at this point, most will suffer from questions like "Am I doing it right still? What if that will fail later? Which one from the alternative solutions to pick? What naming convention is better to follow?".
As heavily subjective topics, they can cost you a ton of time to consider, argue about, and reinvent wheels.
This is where the Clean Code book comes in handy, as the essence of 40+ years of experience in the enterprise field.
Once you read and try to follow, you'll get an answers for most of the common questions, defined priorities and aligned terminology with developers across the globe.
---
<font style="color: #F86759">Contributors:</font> *[[Eugene]]*
<font style="color: #F86759">Last edited:</font> *2024-02-27*