One of the most important goals of any professional software developer is to continuously improve the quality of their work. However, only teamwork can be used in one place to improve software quality. Code review is one of the most important ways to achieve this goal. We often hear team members say:
"Code review for this project is a waste of time ."
"I don't have time to review code ."
"The press conference was delayed because my mean colleague hasn't reviewed my code ."
"Do you believe my colleagues have asked me to change my code? I still need to change the code that is so elegant and perfect ."
Why do we need code review?
One of the most important goals of any professional software developer is to continuously improve the quality of their work. However, only teamwork can be used in one place to improve software quality. Code review is one of the most important ways to achieve this goal. In particular, code reviews can:
Discover defects and better solutions from another perspective.
Make sure at least one other person is familiar with your code.
Read the code of senior developers to help train new employees.
Promote knowledge sharing.
Encourage developers to better write code and solve problems in code, so as not to be pulled out during review.
Complete code review
However, these goals are hard to achieve unless you can spend time and effort on code review in a thorough and thorough manner.
In my opinion, about 25% of the original development time should be spent on code review. For example, if a developer needs two days to implement a program, it takes about four hours to review the program.
Of course, time is not the most important. The key is to check the code correctly. You must understand the code you are reviewing. This means that you not only need to know its syntax, but also need to understand how the code is integrated into the application environment and become part of the component or library. If you cannot grasp the meaning of each line of code, your review will not be in place and will not be very valuable. This is also why code reviews that are well executed are mostly not completed quickly: because we need time to study various types of code, such as code that triggers a given function to ensure correct use of third-party APIs.
During the review, in addition to looking for code defects and other issues, you should also ensure that:
Includes all necessary tests.
An appropriate design document has been written.
Even developers who are good at writing tests and documents will forget to update the code when changing the code. During code review, it should be ensured that such materials will not become useless over time.
Avoid excessive code review
Developers should try to clear the backlog of review tasks. One way is to review the code in the morning and complete the review task before starting your own development work. You can also review the code before or after lunch or at the end of the day. All in all, you should regard the code as part of your daily work, rather than the burden of your work, so you should avoid:
There is no time to process the backlog of review tasks.
Delayed release due to incomplete review.
It's silly to review irrelevant code again, and it's totally different after it is handed over to you.
It was time-consuming to rush through the scene.
Write auditable code
In the case of code backlog and out of control, the reviewer is not the only person in charge. For example, if your colleagues spend a week adding messy code to a large program, it will be difficult to review the released patches, there is too much content to understand and study. Even the purpose and basic architecture of the code are in the fog. This is not code writing.
Some preparation is required before you compile the auditable code. If you need to make some tricky architectural decisions, it is best to discuss it with reviewers first. This will make your code easier to review and understand, because they know in advance what you want to implement and how to implement the plan. This can also avoid the case where the reviewers will come up with a completely different and better method and you will have to rewrite a large piece of code.
The project architecture should be described in detail in the design document. This is important because it allows new project staff to understand the existing code library more quickly and helps reviewers better complete their work. In addition, unit tests allow reviewers to better understand the use of each component.
If your patch contains third-party code, submit it separately. Imagine if 9000 lines of jQuery are inserted in the middle of the code, which greatly increases the difficulty of review!
One of the most important steps to create a review code is to annotate your code review. This requires you to pre-review and add comments to what you think helps reviewers understand. I found that the annotated code review takes a relatively short time (usually several minutes ). Of course, code comments should still be used as appropriate. In addition, some research shows that developers may also find many defects when commenting on code.
Code reconstruction
Sometimes, we have to refactor the code library. If it happens to be a large application, it may take several days (or even more) and produce a large number of patches. In this case, it may be impractical to perform code reviews for standard processes.
The best solution is to gradually refactor the code. First, give a reasonable scope, determine the corresponding code library, and then make rectification and reconstruction in the target direction. After the first part is completed, review and release it, and then rebuild the second part ......, Until all are completed. This step-by-step method may not always be feasible, but if we use this method in thinking and planning, we can avoid restructuring large-scale monolithic patches. Of course, this method may require more refactoring time, but it also produces higher quality code and easier review processes.
If the incremental refactoring code is still not feasible, another solution is Pair Programming.
Dispute resolution
There is no doubt that every member of the team is a talented person, but this can easily lead to different opinions in the face of specific coding problems. As developers, we should be open-minded and be able to accept the comments from reviewers modestly.
As a reviewer, he must speak politely. Before making a suggestion, consider whether your opinion is really better or just because you have different tastes. If the code area you choose does need improvement, the entire persuasion process will be much simpler. In this case, "it is worth considering ......", "Someone suggested ......", Rather than "the algorithms I write with my eyes closed can be more efficient than yours ."