•  Back
  • Code Review Guidelines — Beyond the “what”

    What does Software Engineering research can tell us about code reviews?

    Code reviews and code inspections are highly efficient in fixing defects. A code review can identify from 20% to 35% of the defects, while a code inspection can identify from 45% to 70% of the defects (Caper Jones, Software defect-removal efficiency, IEEE Computer, April 1996, pp. 94–95, DOI 10.1109/2.488361, ISSN 1558–0814).

    It is also much cheaper than finding defects with tests. One of the main reasons is that when you find a defect, you are already looking at the fault, the lines that need to be fixed. When you find a defect using tests, you just found the failure. Localizing the fault in the code usually takes a lot longer than actually fixing it.

    Many exciting results are relevant to be shared. Many are related to the amount of time it takes to review a request and the odds of it being accepted. However, often these studies are based on open-source development, where a request may never be accepted. This blog is focused on the corporate environment, and if you are making a change, it will have to meet the standards to be approved.

    In this context, I’ve selected a few of the most interesting findings.

    Recommendations based on the scientific literature

    • Test-driven reviews: only ONE of the reviewers (max) should start the code review by reviewing the tests.
      A study about test-driven code reviews revealed that reviewing the tests first increases the number of defects found on tests, doesn’t affect the number of defects identified in the code but decreases the number of findings related to code maintainability.
      D. Spadini, F. Palomba, T. Baum, S. Hanenberg, M. Bruntink and A. Bacchelli, “Test-Driven Code Review: An Empirical Study,” 2019 IEEE/ACM 41st International Conference on Software Engineering (ICSE), 2019, pp. 1061–1072, DOI: 10.1109/ICSE.2019.00110.
    • Rebasing: Rebasing after the code review affects the results negatively and should be avoided.
      A 2019 paper published in the 19th International Working Conference on Source Code Analysis and Manipulation reports that rebasing the code invalidates the code changes over 34% of the time, tampering with the code reviews. Rebasing should be avoided or done before the code review.
      M. Paixao and P. H. Maia, “Rebasing in Code Review Considered Harmful: A Large-Scale Empirical Investigation,” 2019 19th International Working Conference on Source Code Analysis and Manipulation (SCAM), 2019, pp. 45–55, DOI: 10.1109/SCAM.2019.00014.
    • Reviewers assignment: Code reviews should be performed by at least two engineers who have not participated in the code development.
      A study about code review practices confirms the expectations that code with less review coverage, fewer reviewers, and less experienced reviewers contribute negatively to post-release defects.
      McIntosh, Shane & Kamei, Yasutaka & Adams, Bram & Hassan, Ahmed E.. (2015). An empirical study of the impact of modern code review practices on software quality. Empirical Software Engineering. 21. 10.1007/s10664–015–9381–9.
    • Questions: As a reviewer, your questions are welcome. Avoid criticizing in the form of a question. Also, avoid rhetorical questions — if you have a point to make, be direct. What-if questions about possible scenarios are usually the most useful ones.
      Another study examines the types of questions made by reviewers and how beneficial they are.
      F. Ebert, F. Castor, N. Novielli and A. Serebrenik, “Communicative Intention in Code Review Questions,” 2018 IEEE International Conference on Software Maintenance and Evolution (ICSME), 2018, pp. 519–523, DOI: 10.1109/ICSME.2018.00061.

    Gray literature

    Gray literature is composed of publications that did not go through peer review and are published in non-scientific texts, like whitepapers, blog posts (such as this), books (anyone can write and publish a book), Wikipedia, etc.

    • Security: OWASP Code Review Guide
    • Size of changes: The code should not exceed 400 lines. If it is longer than that, break the request into smaller parts.
    • Duration of review: The review should not take about 12 minutes for every 100 lines of code and no more than 1h. If it takes more than that, take breaks, or you will find fewer issues.
      A study on Cisco’s computer-based audio and video teleconferencing solution(MeetingPlace) was conducted in 2006 over a period of 10 months. 50 developers on three continents reviewed every code change before it was checked into version control. Data collected from 2500 code reviews indicate that a code review should not take more than 1h and should not have more than 400 lines of code. The pace of the review should be about 12 minutes for every 100 lines of code maximum. Anything more than that results in less effective code reviews.

    My recommendations

    • Split the refactoring: If you refactored the code, it is strongly suggested that you create a separate review for the refactoring changes. The changes in a code review should have an identifiable intention, a goal. If you made just a few refactoring changes because you needed them to make the implementation work, it’s ok to leave it under the same code review.
      However, if you decided to refactor other parts of the code that did not have to be changed necessarily, I’d recommend creating a separate code review for that refactoring.
    • WIP feedback: If the author feels that the feedback can help them decide how to implement the changes, I strongly encourage them to request an early review. An early review is not seeking approval to be merged, just feedback and suggestions.
    • Responsiveness: Reviews should be done on the same day they were requested or the next day maximum, allowing the author to finalize his work, reduce context-switching, and deliver on time.
    • LTGM/Approval: Make it clear if you mean that you are confident that the necessary changes will be done correctly or if you mean that the changes suggested are optional.
    • Prioritize a Subject Matter Expert for that area of the code or component under change (as long as they are not the author), especially if the SME is a member of the same team as the author of the changes.
      If the change is of high-risk, high-impact, or change design elements, I strongly advise having an SME as a reviewer, even when the SME is not a member of the same team.

    What are your recommendations? Sound off in the comments! :-)

    If you like this post, please share it (you can use the buttons in the end of this post). It will help me a lot and keep me motivated to write more. Also, subscribe to get notified of new posts when they come out.

  •  Back
  • You might also enjoy