I recently wrote about Five Books That Changed My Programming Style . In the comments, several readers recommended "Clean Code" by Robert S. Martin. As a result, I read the book and found it worthy of an in-depth review.
About the book
« » 2008 , Amazon. , « », Agile Manifesto . 4,4 Goodreads 13 000 . , , .
.
1. —
, — . , ( ) .
Rather, we'll start with a sketch, gradually adding details. We revise, refine and expand - we work at different levels of abstraction until the software works the way we need it. The software is never really finished.
This is the main essence of "Clean Code". Throughout the book, the author pursues the idea that software is art and akin to painting.
Nice code. Source: xkcd
But how do you go from writing simple code to the art of programming?
According to Martin, the main tools at our disposal are continuous refactoring and test-driven development (TDD). They are inseparable from each other, like two sides of a coin. Here are some definitions.
— .
— , , .
, .
- . , .
- , . .
- . .
« , « ». , «», , . ».
, , , , — , , .
2. !
« — . , ».
.
, .
- — 20 10 .
- , .
. , .
. . (SRP).
— « » , . , , , .
3.
« ».
.
, , , . :
:
:
- .
- .
- , .
- , .
.
- Use names that reveal intent - for example, int elapsedTimeInDays, not int days ...
- Use pronounceable names - e.g. Customer, not DtaRcrd102
- Avoid encodings - don't use the m_ prefix and don't use Hungarian notation .
- Choose one word for each concept - don't use different names like fetch, retrieve, get for the same operation in essence.
4. Abstraction is important
Abstraction. Source: Abstruse Goose
According to Clean Code, if we want to ensure that our functions perform only one task, we need to make sure that all the commands in each function are at the same level of abstraction.
Martin illustrates this with the following example from FitNesse :
. — hr , — . , , :
:
- render () hr.
- HtmlTag.
- .
:
« ».
.
5. —
, , , , . , .
, , , , . « », , « , ».
« . , . , ».
.
, , , . , , — . , — .
« » , . , , — , .
, , . , , 13 , . , , , « », 2021 . , .