当前位置:网站首页>Code reading methods and best practices
Code reading methods and best practices
2022-07-24 01:27:00 【Zen and the art of computer programming】

introduction
Reading code is a basic skill of programmers , It is also software development 、 maintain 、 evolution 、 An integral part of the review and reuse process . This paper takes reading code as an independent topic for the first time , Systematically discuss . Open source projects — It is a treasure house that all programmers should cherish .
This article focuses on code , Related knowledge and skills are discussed in detail .“ His shan zhishi 、 Can offend jade ”, Read and learn this article carefully , It can quickly improve readers' code reading skills and skills , Then from the existing excellent code 、 Algorithm 、 The architecture 、 Draw nutrition from design , Improve their own development and design ability .
Code reading has its own set of skills , It is important to be able to determine when to use which technology . This article uses several practical examples , Show readers how to distinguish good ( And bad ) Code , How to read , What should be paid attention to , And how to use this knowledge to improve your code .
Get into the habit of reading high-quality code , It can improve the ability of writing code .
There is a window , Never opened , But it should be closed forever ;
There are some people , Do exist , But we have no chance to meet ;
There is a life , Not yet , But we have left forever .
Good code : Beautiful as music
Good programs are like good music , They are done so skillfully , So perfect , Reflect the beauty without words . Just as good music can change your view of life , Let you re-examine your life , It's the same as people who communicated across time and space many years ago .
As a software developer , Good procedure , What it can bring , Not inferior to the impact of music .
Even harder to come by , These valuable procedures are often not made by " Authority figures "、" Renowned experts at home and abroad " Written by , They are written by ordinary programmers . These programmers are no different from readers . Although these programmers are not all powerful figures , But majors make experts , Focus on a certain field for a long time , We can create a wealth that all programmers should cherish .
Just like the ancient Michelangelo , Compared with those famous bishops at that time 、 For the nobility , How insignificant he is , But to this day , Those people and their names are gone with the wind , The glory of the past faded with time , Michelangelo's name is engraved in the hearts of most even ordinary people , Many people can think of those perfect sculptures ---" David "、" Genesis ". This honor may be unparalleled in the history of literature and Art .
Everything changes from quantity to quality
We are programmers .
Our work ( In many cases , And our passion ) Is to create new things by writing code .PRD、UED draft 、 Lots of illustrations 、 Detailed project schedule 、 Piles of thick design documents cannot meet the needs of users . These are wishes , Design , Express what we want to achieve . Only by writing code can we deliver something that really meets the needs of users : Code is real 、 Really 、 Down to earth .
I'm afraid , No great novelist , Never read anyone else's work , No great painter , Never studied other people's paintings , There is no skilled surgeon , I have never seen how to operate on the same thing , No captain is not the first to watch how to actually operate from the co pilot's position .
Analogy from here to there , We can easily understand : The way to write great code is to read it , Read a lot of code : High quality code 、 Low quality code ; Assembly language code 、C Code 、C++ Code 、Java Code 、PHP Code 、Go Code 、Kotlin Code 、TypeScript Code 、Haskell Code 、Lisp Code , Code written by strangers thousands of miles away , And the code we just wrote last week . because , If not , We will constantly redo what others have already done , Repeating the past has
The successes and mistakes that have happened .
Reading the code
What we observe is not nature itself , It's nature's characteristics under the observation methods we use .
—Werner Heisenberg
Pass the correct code review , You can find 90% Software error .
The time programmers spend on software , More than half are reading 、 Maintain others' code .
Code reading has become a basic skill of software engineers nowadays . Besides , Read the actual 、 Write good code , You can have a deeper understanding of how to construct and write important systems , You can't learn this ability just by writing small programs .
In the work of programmers , A large part of the content is reading 、 understand 、 And modify the original ( Write it yourself and others ) Code . Legacy code continues 、 Inevitable accumulation , Emphasis on software reuse , The high mobility of personnel in the software industry and other factors , Have caused the work to be extremely complex 、 The challenge is great .
Get into the good habit of spending time reading high-quality code written by others .
Just like reading high-quality prose can enrich vocabulary 、 Stimulate imagination 、 Expand your mind , Analyzing the internal structure of a well-designed software system can learn new architectural patterns 、 data structure 、 coding method 、 Algorithm 、 Style and document specifications 、 Application programming interface (API), Even new computer languages .
Read high quality code , It can also improve your coding level .
Code readability
I regret to tell you that , Just recently , I checked my program again ( Quality factor and tic tac toe game ), They didn't
There are comments or documents in any form .
I regret to report that I've just recently looked again at my programs for prime factors and tic-tac-toe, and they are entirely free of any sort of comments or documentation.
—Donald E. Knuth
When writing a program, you should consider making it easy to read , also , Whether the program is easy to read , People need to read them .
Write readable code , To be human and to be self , It is all due to the contemporary , A great good thing that will benefit the future .
reusing
Reading code may also be to find reusable components . But don't expect too much .
The reusability of code is very attractive , But it's hard to master . Lower your expectations and you won't feel disappointed .
Writing reusable code is difficult .
these years , Only a few software can stand the test of time , It is reused in many different solutions . Software components generally need to be expanded gradually , And repeated rewriting to apply to two or three different systems , Will become reusable components .
Software developed for special purposes rarely meets these conditions . actually , According to the software cost model (software cost model), Writing reusable software may increase 50% Development workload of .
Read the code motto
When analyzing a program for the first time ,main Is a good starting point .
To develop , It's a good habit to spend time reading high-quality code written by others .
Pay attention to and pay attention to the special non functional requirements in the code , These requirements may lead to specific implementation styles .
In the search for bug when , Please analyze the code from the manifestation of the problem to the root of the problem . Don't follow irrelevant paths ( Go astray ).
Make full use of the debugger 、 Compiler gives warnings or outputs symbolic code horse 、 System call tracker 、 Logging mechanism 、 Package dump tool and message detection program , location bug The location of .
From feature function description to code implementation , You can use keywords to search for relevant codes .
Refactoring , Start with a working system , And make sure that at the end , The system works properly . A proper set of automated test cases (test case) highly necessary .
When starting a new software system , it is to be noted that , The system is made up of many parts , Not just executing statements . also
Pay attention to the following contents : File and directory structure 、 Generation and configuration process 、 User interface and system documentation .There are many alternative strategies for code reading : Bottom up and top down analysis 、 Apply heuristics and check comments and external documents , It should be based on the needs of the problem , Try all these methods .
Creative code layout can be used to improve the readability of code . You can use spaces 、 Temporary variables and parentheses improve the readability of expressions . You can use good indentation and wise choice of variable names , Improve the readability of poorly written programs .
To write 、 When reading the code , Get into the habit of adding comments .
World affairs , It must be done in detail ; It's difficult in the world , Make it easy . To solve the difficult code, start with the easy part .
Conclusion
The sunset , Make a cup of tea , Close your notebook , Knead the old waist that has been sitting for a long time , Stretch your arms , Look out the window at the distant mountains . Is there some " Picking chrysanthemums under the east hedge , Leisurely see the south mountain " Artistic conception ? It's just that TaoYuanming grows chrysanthemums , What we sow and harvest is code .
边栏推荐
- HCIP第二天笔记
- 使用C语言和libcyaml库解析yaml配置文件
- Form resume
- OSPF (fourth day notes)
- High voltage technology test questions and answers
- SCM learning notes 6 -- interrupt system (based on Baiwen STM32F103 series tutorials)
- BAT代码:批量文件下划线重命名
- Simple Gan instance code
- Explanation of terms in Polymer Physics
- 刚开始使用,请教些问题和教程或分享帖子
猜你喜欢
随机推荐
Explanation and induction of polymer physics terms
Concept, key points and summary of postgraduate entrance examination in Polymer Physics
Preprocessing instruction define, do you really understand?
Hcip day 5 notes
Arm architecture and programming 3 -- key control LED (based on Baiwen arm architecture and programming tutorial video)
High voltage technical examination questions with answers
Kubernetes deployment dashboard (visual interface)
HCIP第六天_特殊区域综合实验
Domestic MCU and SOC are rising, but they are still lagging behind in special fields
HCIP第五天笔记
[cloud native kubernetes] deployment advanced resource object management under kubernetes cluster
OSPF (fourth day notes)
Hcip day 6 notes
vantUI,Axiso,常见问题及使用:
High voltage technology test questions and answers
【云原生之kubernetes】kubernetes集群下的Deployment高级资源对象管理
Idea compiler sets the separation line between methods
Rip (notes of the second day)
How safe is Volvo XC90? 5 seats and 7 seats are available
Talk about moment of inertia









