Home Backend Development PHP Tutorial 5 lessons learned from code review

5 lessons learned from code review

Nov 30, 2016 am 10:34 AM
code review

We often hear team members say:

"Code review on this project is a waste of time."

"I don't have time to do code review."

"The launch was delayed because of my mean colleague My code hasn’t been reviewed yet.”

“Can you believe that my colleague asked me to change my code? Why do we need to change such an elegant and perfect code?”

5 lessons learned from code review

Censorship?

 One of the most important goals of any professional software developer is to continuously improve the quality of their work. But only through teamwork can we focus our efforts in one place and improve software quality. Code reviews are 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 there is at least one other person familiar with your code.

Help train new employees by going through senior developers’ code.

Promote knowledge sharing.

Motivate developers to write better code and solve problems in the code to avoid being caught by others during review.  

 Code review must be thorough

  However, unless you can actually spend time and energy on code review thoroughly, the above goals will be difficult to achieve.

My opinion is that about 25% of original development time should be spent on code review. For example, if it takes a developer two days to implement a program, it should take about four hours to review.

Of course time is not the most important thing, the key is whether you can review the code correctly. You must understand the code you are reviewing. This means that you not only have to know its syntax, but you must also understand how the code fits into the context of the application and becomes part of a component or library. If you can't grasp the meaning of each line of code, then your review will not be adequate and will not be very valuable. This is why well-executed code reviews are mostly impossible to complete quickly: because we need time to study the various codes that trigger a given function to ensure that the third-party API is used correctly.

When reviewing, in addition to looking for code flaws and other issues, you should also make sure to:

Include all necessary tests.

Appropriate design documentation has been written.

Even developers who are good at writing tests and documentation will forget to update the code when they change it. Code reviews should ensure that this information does not become useless over time.

Avoid excessive code reviews

Developers should work hard to clear the backlog of review tasks. One approach is to do code reviews in the morning and get the review done before starting your own development work. Of course you can also review code before or after lunch or at the end of the day. All in all, you should treat code as part of your daily work, not as a burden, so you should avoid:

No time to deal with a backlog of review tasks.

Delay in release due to incomplete review.

I foolishly review the irrelevant code, but it has been changed beyond recognition after being handed over to you.

I went through the motions in a hurry because of time constraints.

Write reviewable code

 When the code backlog gets out of control, the reviewer is not the only one who needs to be held responsible. For example, if your colleague spent a week adding messy code to a large program, the released patch will become difficult to review, there will be too much content to understand and delve into. Even the purpose and basic structure of the code are unclear. This is not what writing code does.

 Before writing reviewable code, you need to do some preparation. If you need to make some difficult architectural decisions, it's best to discuss them with the reviewer first. This will make your code easier to review and understand, because they will already know in advance what you want to achieve and how you plan to achieve it. This also prevents you from having to rewrite a large chunk of your code if a reviewer later comes up with a completely different and better approach.

 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 base more quickly, and it also helps reviewers do their jobs better. Additionally, unit testing allows reviewers to better understand the usage of individual components.

 If your patch also contains third-party code, submit it separately. Just imagine, if 9,000 lines of jQuery were inserted in the middle of the code, would it greatly increase the difficulty of review?

 One of the most important steps in creating reviewable code is to annotate your code reviews. This requires pre-reviewing it yourself and then adding comments where you think it will help the reviewer understand. I've found that code reviews after comments take relatively little time (usually just a few minutes). Of course, code comments should still be used where appropriate. In addition, research shows that developers themselves will find many existing defects when commenting on code.

Code Refactoring

Sometimes, we have to refactor the code base. If you happen to encounter a large application, it may take several days (or more) and generate a large number of patches. In this case, it may be impractical to implement a standard process for code review.

 The best solution is to refactor the code step by step. First give a reasonable scope, determine the corresponding code base, and then make rectification and reconstruction in the target direction. After the first part is done, review and publish it, then refactor the second part... until it's all done. This staged approach may not always be possible, but if we use this approach when thinking and planning, we can avoid large-scale monolithic patches when refactoring. Of course, this approach may require more refactoring time, but it will also produce higher quality code and an easier review process.

  If incremental refactoring of the code is still not feasible, then another solution is pair programming.

Dispute Resolution

There is no doubt that every member of the team is a talent, but this can also easily lead to differences of opinion when faced with specific coding issues. As developers, we should keep an open mind and be willing to accept different opinions from reviewers.

 As a reviewer, you should speak tactfully. Before making a suggestion, consider whether your opinion is truly better or just a matter of different taste. If you choose an area of ​​code that really needs improvement, the whole convincing process will be much simpler. And the words should be said like, "It's worth considering here...", "Someone suggested...", rather than "The algorithm I wrote with my eyes closed can be more efficient than yours."


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Go language for code review practice How to use Go language for code review practice Aug 02, 2023 pm 11:10 PM

How to use Go language for code review practice Introduction: In the software development process, code review (CodeReview) is an important practice. By reviewing and analyzing each other's code, team members can identify potential problems, improve code quality, increase teamwork, and share knowledge. This article will introduce how to use Go language for code review practices, and attach code examples. 1. The importance of code review Code review is a best practice to promote code quality. It can find and correct potential errors in the code, improve the code

How to conduct code review and performance optimization in Java development How to conduct code review and performance optimization in Java development Oct 10, 2023 pm 03:05 PM

How to conduct code review and performance optimization in Java development requires specific code examples. In the daily Java development process, code review and performance optimization are very important links. Code review can ensure the quality and maintainability of the code, while performance optimization can improve the operating efficiency and response speed of the system. This article will introduce how to conduct Java code review and performance optimization, and give specific code examples. Code review Code review is the process of checking the code line by line as it is written and fixing potential problems and errors. the following

Python development experience sharing: how to conduct code review and quality assurance Python development experience sharing: how to conduct code review and quality assurance Nov 22, 2023 am 08:18 AM

Python development experience sharing: How to conduct code review and quality assurance Introduction: In the software development process, code review and quality assurance are crucial links. Good code review can improve code quality, reduce errors and defects, and improve program maintainability and scalability. This article will share the experience of code review and quality assurance in Python development from the following aspects. 1. Develop code review specifications Code review is a systematic activity that requires a comprehensive inspection and evaluation of the code. In order to standardize code review

PHP code review and continuous integration PHP code review and continuous integration May 06, 2024 pm 03:00 PM

Yes, combining code reviews with continuous integration can improve code quality and delivery efficiency. Specific tools include: PHP_CodeSniffer: Check coding style and best practices. PHPStan: Detect errors and unused variables. Psalm: Provides type checking and advanced code analysis.

C# Development Notes: Code Review and Quality Assurance C# Development Notes: Code Review and Quality Assurance Nov 22, 2023 pm 05:00 PM

In the C# development process, code quality assurance is crucial. The quality of code directly affects the stability, maintainability and scalability of software. As an important quality assurance method, code review plays a role that cannot be ignored in software development. This article will focus on code review considerations in C# development to help developers improve code quality. 1. The purpose and significance of review Code review refers to the process of discovering and correcting existing problems and errors by carefully reading and inspecting the code. Its main purpose is to improve the

React code review guide: How to ensure the quality and maintainability of your front-end code React code review guide: How to ensure the quality and maintainability of your front-end code Sep 27, 2023 pm 02:45 PM

React Code Review Guide: How to Ensure the Quality and Maintainability of Front-End Code Introduction: In today’s software development, front-end code is increasingly important. As a popular front-end development framework, React is widely used in various types of applications. However, due to the flexibility and power of React, writing high-quality and maintainable code can become a challenge. To address this issue, this article will introduce some best practices for React code review and provide some concrete code examples. 1. Code style

How to do code reviews and merge requests in GitLab How to do code reviews and merge requests in GitLab Oct 20, 2023 pm 04:03 PM

How to Conduct Code Reviews and Merge Requests in GitLab Code review is an important development practice that can help teams identify potential problems and improve code quality. In GitLab, through the merge request (MergeRequest) function, we can easily conduct code review and merge work. This article explains how to perform code reviews and merge requests in GitLab, while providing specific code examples. Preparation: Please make sure you have created a GitLab project and have the relevant

How to conduct code review of C++ code? How to conduct code review of C++ code? Nov 02, 2023 am 09:12 AM

How to conduct code review of C++ code? Code review is a very important part of the software development process. It can help the development team identify and correct potential errors, improve code quality, and reduce the workload of subsequent maintenance and debugging. For strongly typed static languages ​​like C++, code review is particularly important. Here are some key steps and considerations to help you conduct an effective C++ code review. Set code review standards: Before conducting a code review, the team should jointly develop a code review standard to agree on various errors and violations.

See all articles