当前位置:网站首页>Code Review Best Practices

Code Review Best Practices

2020-11-07 20:15:00 Baoyu

I always thought Code Review( Code review ) Is one of the best practices in software development , Can effectively improve the overall code quality , Find out the possible problems in the code in time . Including images Google、 Microsoft companies ,Code Review It's all basic requirements , The code must be reviewed and approved before merging .

 

But for most of the software development teams I've observed , Do it seriously Code Review Very few , Some are mere formality , Some may not have Code Review Link , Code quality only depends on post test . There are also teams that want to do a good code review , But I don't know how to do it better .

  Online about how to do Code Review There are a lot of articles about , Here I combine some of my own experience , I have also summarized and sorted out Code Review Best practices , I hope I can do well for you Code Review Help .

Code Review What are the benefits ?

Many teams or individuals don't do Code Review, Root still doesn't think it's a meaningful thing , I don't think it's any good . This problem should be viewed from several angles .

  • The first is from the perspective of team knowledge sharing  

In a development team , The level is high or low , Everyone's focus is different . How to help new people grow at a high level ? How to keep everyone familiar with the knowledge beyond their focus areas ? How can someone take over quickly after leaving ? These are the concerns of team managers .

And code review , It's a good way to share knowledge . Through code review , Master can directly point out the problems in novice code , The novice can learn the good practice from the expert's feedback immediately , Grow faster ; Through code review , The front end can also learn the code of the back end , Make function modules A You can understand the function modules of B Of .

Maybe some experts think it's a waste of time for novice code review , I didn't get any . It's not , New people grow up , You can help more experts to share the heavy tasks ; Spend time in code review , Less time to fill in the hole and wipe the bottom ; Good communication skill 、 The ability to find problems 、 Help others grow , All of them are the necessary capabilities for technology to management or technology to a higher level , And through the code review can effectively practice these aspects of the ability .

  • And then there's the code quality perspective

In reality, the project is always short of manpower and schedule , So what's compressed is often automated testing and code review , The result affects the quality of the code , Technical debt due , In the end, we have to double the repayment .

There are also people who hope to test manually after development , But for code quality , Many problems can't be solved if they pass the test , Only through code review . For example, the readability and maintainability of the code , Like the structure of the code , For example, some specific conditions trigger the dead cycle 、 Logic algorithm error , There are also some security vulnerabilities that are easier to detect and prevent through code review . 

Some people think that they don't need code review because of their high level . For a master , Let others review their code , Can let others learn good practice ; At the same time that other people review , When explaining your code to others , It's a review of your own code . It's the same as doing math problems when we were in school , Those who can really get high marks are often those who will check them carefully after they are finished .

  • There is also the perspective of team norms  

Each team has its own code specification , Have their own development specifications based on architecture design , But for a long time , You'll find a lot of non-compliance in the code , There's a lot of code that bypasses architectural design . For example, hard to understand and irregular naming , For example, in the three-tier architecture UI Layer bypasses the business logic layer and directly calls the data access layer code . 

If these code violations are corrected late , It will cost a lot to modify later , And the norms of the team will slowly become meaningless .

Through code review , We can find and correct these problems in time , To ensure the implementation of team norms .

About the benefits of code review , There are still a lot of it , Don't list one by one . Still hope to realize Code Review Like writing automated tests , All belong to the work of sharpening knives and cutting firewood , Put a little time on it , In the future, we will gain code quality , Will save overall development time .

What to do ? 

Now many people have realized Code Review The importance of , I just don't know how to practice , I don't know how it's good Code Review practice .

hold Code Review As a must for the development process, not an option

Long ago , I've tried to use code review as part of the code process , But it's just an option , No, Code Review You can also merge code into master. The result is to think about it and do it Code Review, There are already too many code changes when we go to check , It's very difficult to censor , And even if there's something wrong with the review , It's also hard to modify . 

Our review of the code is now a must for the development process , Every time you develop a new feature or fix Bug, Open a new branch , Branch to merge into master There are two necessary conditions :

  • All automated tests passed
  • There is at least one person Code Review adopt , If it's new PR, There must also be senior programmers Code Review adopt

picture source :How to Do Code Reviews Like a Human

So the Code Review As a required option in the development process , It's a good guarantee that the code has Code Review. And the process of requiring code review before merging like this , The benefits are obvious :

  • Because every time before merging to do code review , In this way, the amount of code for a general review will not be too large , It's not too much pressure for censors either
  • If in Code Review When I found a problem , The censor wants the code to merge as soon as possible , It will also actively revise the problems reviewed , Don't be too conflicted with the results of the review

If you think Code Review Difficult to carry out , Try to put Code Review Become a must for your development process .

hold Code Review Become a development culture, not just a system

hold Code Review As a required part of the development process , Do not represent Code Review This can be done very well , because Code Review Implementation , A lot of it depends on the scrutiny of the censors , And the active cooperation of the examinee , You can't have one without the other !

If only as a process system , Then it may become mere formality . The end result is that there seems to be Code Review, But no one is scrutinizing it , Just look at it and it passes , Or if you find a problem, you don't want to change it .

I really want to Code Review This is a good thing , Must let Code Review Become a team culture , Developers take it from the bottom of their hearts , And take it seriously .

To form such a culture , Not so easy , It's not as hard as you think , For example, you can refer to :

  • First , It's important for developers to realize Code Review This is for myself 、 Benefits for the team
  • then , We have to have a few people to set an example , The power of example is very important
  • also , For managers , What do you inspire , Often get what
  • Last , Like writing automated tests , hold Code Review As part of the development task , Give both the examiner and the examinee special time to do it , You can't just think of a horse running fast and be reluctant to feed it  

How to form such a culture , If you want to , There are many ways to try . Only really let everyone agree and practice , To do well Code Review This matter .

some Code Review Experience and skill

Doing a good job Code Review In this matter , There are also some experience skills to refer to .

Choose what tools to assist in CODE REVIEW?

Now many source control tools come with Code Review Tools , A typical image Github、Gitlab、 Microsoft Azure DevOps, Especially like Gitlab, You can also build your own local environment , According to their own needs, flexible configuration . 

With what kind of development process is better ?

image Github Flow This branch based development process is particularly suitable for matching Code Review Of . In fact, no matter what kind of development process , The key is to merge the code into master( The trunk ) Before , Do it first Code Review.

What an emergency , What to do with code review ? 

Although in principle , It has to be Code Review To merge , But sometimes there are emergencies , For example, online fault patches , And no one else is online , So in this case , It's better in the task management system , Create a Ticket, For follow-up tracking , Make sure that you make up for Code Review, Also on Code Review The result is a subsequent code update .

Design before coding

Some new people find their code submission PR(Pull Request) after , I'll get a bunch of Code Review opinion , A lot of changes have to be made . It's mostly because before you start , Didn't design well , After doing it, I found that there were many problems . 

It's recommended that before making a new feature , Write a simple design document , Express your design ideas , Find a senior to do a design review for you first , Find design problems . There's no problem with the design , Start developing , So to Review When , Relative problems will be much less .

The code is committing CODE REVIEW Before , The author must first of all REVIEW And a test  

When I was doing code reviews , Sometimes I find some very obvious problems , Some of them have not even tested themselves , Just wait for someone else Code Review And tests to help find problems . This kind of dependence is very irresponsible not only to oneself but also to the team . 

A good developer , The code is committing Code Review Before , It must be that you have to be the first Review Again , Write the automation test code that should be written , Run the basic test cases by yourself . 

I'm talking about PR, There's a requirement to be in PR Add screenshots or video recordings to the description of , It's for screenshots or video recording , Make sure to submit PR People who have tested themselves first . It's also an effective aid .

PR smaller  

Doing it Code Review When , If there are a lot of file changes , that Review It's very difficult to get up , But if PR The relatively small , It's relatively easy Review, It's also easy to find problems in the code .

So I'm submitting PR when ,PR smaller , If it's a big change , Then it's better to submit in batches , To reduce the pressure on censors .

Rate comments

Doing it Code Review when , Comments need to be added for lines of code that have been reviewed as problematic , If it's just a comment , Sometimes it is difficult for the examinee to identify the meaning of the comment , Is it necessary to modify .

Suggestions can be made to Review The comments are graded , Different levels of results can be marked with different Tag, for instance :

  • [blocker]: Add a... Before the comment [blocker] Mark , Indicates that the problem with this line of code must be modified
  • [optional]: Add a... Before the comment [optional] Mark , Indicates that the problem of this code line can be modified or not
  • [question]: Add a... Before the comment [question] Mark , Indicates that you do not understand this line of code , There are questions to ask , The examinee needs to reply to the questions for clarification  

A rating like this can help the examinee visualize Review result , Improve Review efficiency .

Comments should be friendly , Avoid negative words ; There are some unclear problems to communicate face to face  

Although comments are the main Code Review Way of communication , But don't rely too much on , Sometimes face-to-face communication is more efficient , It's also easy to eliminate misunderstandings .

Another civilized language , Don't use negative words . 

summary  

Code Review Is a very good development practice , If you haven't started yet , May as well practice gradually ; If it has been done, the effect is not good , May as well contrast , Let's see if there is a Code Review As a must for the development process, not an option ? Have you put Code Review Become a development culture, not just a system ?

版权声明
本文为[Baoyu]所创,转载请带上原文链接,感谢