Code Review: Successful Experience



On the Internet there is a lot of information on the review code:


Yes, of course, there are books on this subject. In short, this article outlines how the code review is organized at Palantir. In those organizations whose culture does not accept such a peer review, it may be useful to first read Karl Widgers’s brilliant essay “ Review of a Code with a Human Face” and then try to follow this guide.

This text is taken from recommendations for quality improvement, based on working with Baseline , our tool for quality control of Java code. It covers the following topics:


Transferred to Alconost


Comic XKCD 'Code Quality', copied under CC BY-NC 2.5 license

Motivation


We are engaged in the review of the code (RC) to improve the quality of the code and thus want to positively influence the team and corporate culture. For example:


- When RK, it is explicitly stated what part of the functionality was added / changed / deleted, so that in the future the team members could rely on the work already done.

- The author of the code can use a technique or algorithm in which the reviewers themselves will learn something. Overall, a code review helps raise the bar for quality throughout the organization .

- Reviewers may know something about programming techniques or about the code base itself that will help improve or consolidate the changes made; for example, someone can at the same time develop or refine the exact same opportunity.

- Positive contact and communication reinforces social connections between team members.


What to look for


There is no clear answer to this question, and each development team is required to agree on its own approach. In some teams they prefer to check any changes made to the main branch of the development, while in others they take into account the “threshold of triviality”, beyond which verification is mandatory. In this case, one has to strike a balance between efficient use of the programmer’s time (both the author and the code reviewer) and maintaining the quality of the code. In some strict contexts, it is sometimes necessary to check everything in the code, even the most trivial changes.

The rules for reviewing the code are the same for everyone: even if you are the most senior developer in the team, this does not mean that your code does not require a review. Even if (sometimes it happens) the code is flawless, the review opens up opportunities for mentoring and cooperation, and, at a minimum, helps to look at the code base from different points of view.

When to check


The code review should occur after successful completion of automatic checks (tests, design, other stages of continuous integration), but before the merging of the new code with the main repository branch. Usually we do not resort to formal verification of the total changes made to the code since the last release.

For complex changes that should be added to the main branch of the code as a single block, but so extensive that they cannot be covered in a single check, try a step-by-step review. We create a primary feature / big-feature branch and a number of secondary ones (for example, feature / big-feature-api, feature / big-feature-testing, etc.), each of which encapsulates a subset of common functionality, and each such branch check with the main branch feature / big-feature. After all the secondary branches are merged into feature / big-feature, create a review of the code for infusing the latter into the main branch.

Preparing the code for review


The author of the code is obliged to provide the revision code in a digestible form so as not to take too much time from the reviewers and not to demotivate them:


- Changes related to refactoring usually affect multiple lines of code in many files - therefore, this code will not be checked so carefully . Unplanned changes in behavior can leak into the code base, and no one will even notice.

- Major changes associated with refactoring violate the mechanisms of movement, selection and other "magic" associated with the control of source codes. It is very difficult to undo such a change in behavior that was made after the completion of refactoring, which covered the entire repository.

- The expensive working time that a person spends on reviewing code should be spent on checking software logic, and not on disputes about style, syntax, or code formatting. We prefer to solve such questions with the help of automatic tools, in particular, Checkstyle , TSLint , Baseline , Prettier , etc.

Commit messages


The following is an example of a competent commit message, which is maintained in accordance with a widely used standard:

 ( 80 )    ,  ,   .   72 .          ,    —   .   ,       (   );     ,  rebase,    .  -   : « »,   « »   « ».      -,  , ,  git merge  git revert.      .    . 

Try to describe the changes made at the commit, and how exactly these changes are made:

 > .   .    . > .  jcsv-     IntelliJ 

Reviewer Search


Usually the author of a commit looks for one or two reviewers familiar with the code base. Often in this capacity act as a project manager or senior engineer. It is advisable for the project owner to subscribe to his own project in order to receive notifications about new code reviews. With the participation of three or more reviewers, reviewing a code is often unproductive or counterproductive, since different reviewers may suggest mutually contradictory changes. This may indicate a fundamental disagreement about the correct implementation, and such problems should be solved not during the review of the code, but at an extended meeting, in which all interested parties participate in person or in the form of a video conference.

Execute Code Review


A code review is a synchronization point between different team members, so potentially it can stop work. Therefore, review of the code should be operational (take about a few hours, not days), and members and team leaders should be aware of how long it will take to check, and prioritize the time allocated to it accordingly. If it seems to you that you do not have time to complete the review on time, immediately tell the commit author about it so that he can find a replacement for you.

The review should be fairly solid so that the reviewer can explain the change to another developer with sufficient detail. So we will ensure that the details of the code base were known at least to two, and not to one.

You, as a reviewer, must persist in adhering to code quality standards and keep it high. The review of the code is more an art than a science. You can learn this only in practice. An experienced reviewer should first try to let less experienced colleagues change and let them review first. If the author followed the above rules (especially those concerning self-checking and pre-launching of the code), then the reviewer should pay attention to this when reviewing:

purpose



Implementation



Readability and style



Convenience support



Do not forget to praise concise / readable / effective / beautiful code. On the contrary, rejecting or rejecting the code proposed for the review is not rude. If the change is redundant or insignificant - reject it, explaining the reason. If the change seems unacceptable to you because of one or several fatal errors - reject it, again, reasonably. Sometimes the best outcome of a review of a code is “let's do it completely differently” or “let's not do it at all”.

Respect the reviewer. Although, the position of the opponent is sound, you did not realize this opportunity, and you cannot decide everything for it. If it is not possible to come with the reviewer to a common opinion about the code, arrange the consultation in real time and listen to the opinion of a third person.

Security


Ensure that all API endpoints have adequate authorization and authentication that conforms to the rules adopted in the rest of the code base. Look for other common flaws, for example: weak configuration, malicious user input, missing log entries, etc. If in doubt, show the code being reviewed to a security professional.

Comments: brief, polite, incentive


The review should be brief, sustained in a neutral style. Criticize the code, not the author. If something is unclear - ask for clarification, but do not consider that the whole thing is in the ignorance of the author. Avoid possessive pronouns, especially in evaluative judgments: “ my code worked before your changes,” “there is a bug in your method,” etc. Do not chop off: "it will not work at all ", "the result is always wrong."

Try to distinguish between recommendations (eg, “Recommendation: remove the method, so the code will become clearer”), necessary changes (eg, “Add Override ”) and opinions that require clarification or discussion (eg, “Is this behavior true ? If so, please add a comment explaining the logic "). Try putting links or pointers to a detailed description of the problem.

After reviewing the code, indicate how detailed the reaction to your comments you expect from the author, would you like to repeat the review after the changes have been made, for example: “You can safely upload the code to the main branch after these minor corrections” or “Please note my comments and let me know when I can see the code again. ”

Reaction to the review


The review of the code, in particular, is intended to improve the change request proposed by the author; so do not take the recommendations of the reviewer at bayonets; take them seriously, even if you do not agree with them. Reply to any comment, even the usual “ACK” (approved) or “done” (done). Explain why you made this or that decision, why you have certain functions in the code, etc. If you cannot reach a common opinion with the reviewer, arrange a real-time consultation and listen to the opinion of an outside expert.

Corrections should be fixed in the same branch, but in a separate commit. If you merge commits at the review stage, it will be more difficult for the reviewer to track changes.

The policy of merging code in different teams is different. In some companies, the merger of the code is trusted only to the project owner, in others the participant can also do this after his code has been reviewed and approved.

Review code tete-a-tete


As a rule, asynchronous diff-like tools such as Reviewable, Gerrit, or GitHub are great for reviewing code. If we are talking about a complex review or review, the participants of which differ greatly in the level of experience or professionalism, it can be more effective to conduct a review personally - either sitting together in front of a single monitor or projector, or remotely, via video conference or screen sharing tools.

Examples


In the following examples, reviewer comments in listings are entered using

 //R: ... 


Contradictory naming


 class MyClass { private int countTotalPageVisits; //R:    private int uniqueUsersCount; } 

Inconsistent method signatures


 interface MyInterface { /**  {@link Optional#empty},  s   . */ public Optional<String> extractString(String s); /**  null,  {@code s}   . */ //R:    :    // Optional<> public String rewriteString(String s); } 

Library use


 //R:     MapJoiner   Guava String joinAndConcatenate(Map<String, String> map, String keyValueSeparator, String keySeparator); 

Personal preferences


 int dayCount; //R: :   numFoo   fooCount;   ,          

Bugs


 //R:    numIterations+1 –    ? //    –     numIterations? for (int i = 0; i <= numIterations; ++i) { ... } 

Architectural considerations


 otherService.call(); //R: ,      OtherService.    ? 

About the translator

The article is translated in Alconost.

Alconost is engaged in the localization of games , applications and websites in 70 languages. Language translators, linguistic testing, cloud platform with API, continuous localization, 24/7 project managers, any formats of string resources.

We also make advertising and training videos - for websites selling, image, advertising, training, teasers, expliners, trailers for Google Play and the App Store.

Read more

Source: https://habr.com/ru/post/413589/


All Articles