A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. title: How to perform a good code review
  2. url: https://blog.alphasmanifesto.com/2016/11/17/how-to-perform-a-good-code-review/
  3. hash_url: 4f5f1314047ae271ac5f080f6c43bff4
  4. <p style="text-align: justify;">I recently wrote about <a href="https://blog.alphasmanifesto.com/2016/07/11/how-to-create-a-good-pull-request/">how to create a good pull request</a>, this is, how to make your code changes easy to review and discuss. Now we&#8217;re going to talk about the second part: reviewing someone else&#8217;s code. This puts you on the reviewers side, and hopefully the person submitting code did follow our guidelines to make your life easier.</p>
  5. <p style="text-align: justify;">There are several approaches you can take to review the code, but we&#8217;re going to enumerate a checklist that you could use to minimize the usage of your time and the efficiency of the code review.</p>
  6. <p style="text-align: justify;"><span id="more-5613"></span></p>
  7. <h2>Disclaimer</h2>
  8. <p style="text-align: justify;">Every code, every project every team is different. This is just a set of guidelines, not a set of rules that should be applied dogmatically.</p>
  9. <p style="text-align: justify;">Also, note that code reviews are pretty much a matter of looking into code and of communicating what you found in a review. As such, most of what is mentioned here will circle around how the communication should be rather than how code should look like. Determining what is appropriate for the code in your project depends on your situation.</p>
  10. <h2>1. Make the most involved person be the one answering questions</h2>
  11. <p style="text-align: justify;">Assuming you&#8217;re not the person that actually wrote the code (because you&#8217;re reviewing it), you need to accept that you don&#8217;t have all the context of what made it happen or what gave it its shape. With that, don&#8217;t assume everything is a mistake, but it&#8217;s ok to point out the difference with what you expected, and let the other person explain themselves.</p>
  12. <p style="text-align: justify;">Keep this in mind as your approach to every question or detail:</p>
  13. <ol>
  14. <li style="text-align: justify;"><strong>If it&#8217;s definitely a problem, point out why is it a problem.</strong> Explain what are the consequences of the change you find problematic. If you have a solution, propose it. If you don&#8217;t but you have a guideline on how to approach the problem, mention it. If you think a particular design pattern would help, mention it. If you think there&#8217;s another feature or portion of the code that already solves this problem, point it out. Whatever you can point to the solution will help the provided solution not deviate from what is acceptable to you.</li>
  15. <li style="text-align: justify;"><strong>If it&#8217;s not a problem, don&#8217;t point it out like one.</strong> Make sure that your doubts and suggestions are perceived as that: doubts and suggestions. If the reviewed person thinks that something is a problem they&#8217;ll go ahead and change it, maybe without previous discussion. This may, ironically, become a problem if the code keeps changing too much from what it was intended. Or worse, you could frustrate the reviewed person by requesting too many iterations on the same piece of code because you weren&#8217;t completely clear on what needed to be done. Just be honest with yourself and identify what of the things you don&#8217;t like are based on taste and which ones are real problems that should be fixed.</li>
  16. <li style="text-align: justify;"><strong>Let questions be questions.</strong> Let the other person answer them, instead of hinting at an answer yourself. This is, don&#8217;t make rhetoric questions, and don&#8217;t corner the other person into providing the answer that you want them to give.</li>
  17. <li style="text-align: justify;"><strong>Let them do the investigation.</strong> They&#8217;re most comfortable with the code and they know it better than you. They&#8217;ll know better and find the answer to the questions quicker that you probably will. However, there might be value in investigating yourself, if you want to prioritize understanding the code inner workings besides the time of the review itself.</li>
  18. </ol>
  19. <h3>Bad examples</h3>
  20. <blockquote>
  21. <p style="text-align: justify;">Please change this so it performs better.</p>
  22. </blockquote>
  23. <ol>
  24. <li style="text-align: justify;">It assumes that there&#8217;s something performing poorly.</li>
  25. <li style="text-align: justify;">Even worse, it doesn&#8217;t point out what <em>it</em> is, assuming that the reviewed person already knows what you&#8217;re referring to.</li>
  26. <li style="text-align: justify;">Why is it performing poorly? How should it be changed?</li>
  27. <li style="text-align: justify;">Is performance even a problem? What is this impacting? (Mention it.)</li>
  28. </ol>
  29. <h3>Good examples</h3>
  30. <blockquote><p>I believe that this SQL query has the correct result set, but I think it may be performing table scans or it may not be making a good use of indexes, but</p>
  31. <p style="text-align: justify;">I&#8217;m not entirely sure. Can you check the execution plan and see if the performance in database can be improved in any way? Maybe we can change the query, maybe we can add indexes. Remember that users will be impacted each time they see the main listing.</p>
  32. </blockquote>
  33. <p style="text-align: justify;">It acknowledges what is right about this piece (the SQL result).</p>
  34. <ol>
  35. <li style="text-align: justify;">It mentions a reasonable doubt and expresses it as a doubt. (&#8220;I&#8217;m not sure&#8221;)</li>
  36. <li style="text-align: justify;">It indicates what may be the source of the problem (&#8220;table scans&#8221;, &#8220;unused indexes&#8221;)</li>
  37. <li style="text-align: justify;">It suggests a course of action (&#8220;check the execution plan&#8221;)</li>
  38. <li style="text-align: justify;">Suggests possible corrections (&#8220;change the query&#8221;, &#8220;add indexes&#8221;).</li>
  39. <li style="text-align: justify;">It indicates what the real-life impact of such a change will be (&#8220;users listings&#8221;).</li>
  40. </ol>
  41. <h2>2. Always provide a reason</h2>
  42. <p style="text-align: justify;">When you&#8217;re suggesting changes or pointing out a problem, you need to specify what the reason there is behind it. The point of doing so is allowing the other people involved in the conversation search the right solution along with you or come up with solutions themselves.</p>
  43. <p style="text-align: justify;">This is specially important when you don&#8217;t mention explicitly what the intended approach should be, and so just pointing out a problem does not help them avoid the underlying reason.</p>
  44. <h3>Bad examples</h3>
  45. <blockquote>
  46. <p style="text-align: justify;">Typo here.</p>
  47. </blockquote>
  48. <p style="text-align: justify;">This is a bad example because if the one that made the typo wasn&#8217;t aware of it, it&#8217;s likely that they won&#8217;t be able to recognize it. Unless, of course, it&#8217;s really <em>obvvvious.</em> Regardless, it&#8217;s always a good idea to provide a good spelling.</p>
  49. <blockquote>
  50. <p style="text-align: justify;">Need to add authentication here.</p>
  51. </blockquote>
  52. <p style="text-align: justify;">&#8220;Here&#8221; is quite ambiguous, but still, unless the developer being reviewed is already in line to what the process of adding authentication to a part of the code means, it&#8217;s likely that they will either end up replicating something that&#8217;s close to this portion of code, or just trying to guess what the reviewer meant. Being specific about what you had in mind the code should have helps reviewed code come closer to what you want it to be.</p>
  53. <h3>Good examples</h3>
  54. <blockquote>
  55. <p style="text-align: justify;">Here it spells &#8220;typ&#8221; but it should say &#8220;type&#8221;.</p>
  56. </blockquote>
  57. <p style="text-align: justify;">Correction from the first bad example: it shows what the typo is, it should how it should be corrected.</p>
  58. <blockquote>
  59. <p style="text-align: justify;">This will allow any non-authenticated user to invoke this method. This could create a security problem where we have non-authenticated third parties changing the internal state of the system. Please add the authentication attribute to prevent this situation. You can follow the same pattern that we have in UserController.cs.</p>
  60. </blockquote>
  61. <p>Explains the problem, hopefully preventing further instances from occurring now that the developer understands the rationale behind it, and provides guidance into how to solve it.</p>
  62. <h2>3. What to check for</h2>
  63. <p style="text-align: justify;">Here is a long list that you should keep in mind when reviewing. For all of this, if the answer does not leave you comfortable, you found yourself a problem and you want to leave feedback to be corrected in that pull request.</p>
  64. <p style="text-align: justify;">However, also remember that you should pick your battles and that being right about every little discussion is not really <em>that</em> important, but rather what you&#8217;re balancing to have a good quality product. (Are you going to lose a good developer because you disagree on indentation? Hold your horses.)</p>
  65. <p style="text-align: justify;">You may choose to review all the code for the first point, then the second and so on, or you may choose to use this as a checklist on every piece of code that you review. I have ordered them from most important to less important, in my personal opinion:</p>
  66. <h3>3.1. Quick checklist</h3>
  67. <ol>
  68. <li style="text-align: justify;">Is the change introduced feasible?
  69. <ol>
  70. <li>Does it do what it&#8217;s supposed to do? Maybe check with the original requirement, if it&#8217;s written down somewhere.</li>
  71. <li>Are the assumptions and constraints that it makes to work assumptions that can be taken in this system?</li>
  72. <li>If it causes impact to users, is the impact acceptable? (E.g. a new large JavaScript library being added to a web app.)</li>
  73. <li>Does the change avoid incurring into costs? (Servers, licenses, processor usage, etc.)</li>
  74. <li>Is the change legal? (Does it avoid unpaid proprietary libraries, code taken from another source, incompatible licenses, unprotected data, etc.?)</li>
  75. </ol>
  76. </li>
  77. <li style="text-align: justify;">Does it have the right technical direction?
  78. <ol>
  79. <li>Is the current design appropriate for the solution being sought?</li>
  80. <li>Does it introduce the right amount of complexity for the change required?</li>
  81. <li>Does it introduce the lowest amount of failure points possible?</li>
  82. <li>Is it flexible? Is it flexible in the direction that the product will evolve?</li>
  83. <li>Does it avoid introducing third-party libraries and code when a simpler or more appropriate solution will do?</li>
  84. </ol>
  85. </li>
  86. <li style="text-align: justify;">Does it handle errors and exceptions appropriately?
  87. <ol>
  88. <li>If they should, are they logged?</li>
  89. <li>If they should, are they shown to the user with the right message?</li>
  90. <li>If they should, are they relayed back to the development team?</li>
  91. <li>Are they handled? Are they handled in the right section of code?</li>
  92. </ol>
  93. </li>
  94. <li style="text-align: justify;">Does it have the right amount of instrumentation?
  95. <ol>
  96. <li>Does it instrument meaningful data?</li>
  97. </ol>
  98. </li>
  99. <li style="text-align: justify;">Is it secure?
  100. <ol>
  101. <li>Does it avoid disclosing any data that should be kept private?</li>
  102. <li>Does it encrypt data that should not be plainly stored or transmitted?</li>
  103. <li>Does it sanitize or validate inputs or third-party systems?</li>
  104. </ol>
  105. </li>
  106. <li style="text-align: justify;">Does it avoid technical debt?
  107. <ol>
  108. <li>If not, are the debt items documented close to the code with their reason?</li>
  109. <li>Are the reasons for not addressing them right now valid?</li>
  110. <li>Is the technical debt introduced acceptable?</li>
  111. </ol>
  112. </li>
  113. <li style="text-align: justify;">Is it tested?
  114. <ol>
  115. <li>Does it have validation/whitelisting/blacklisting for input data?</li>
  116. <li>Does it use the right size of variables for data involved? (E.g. <em>short amountOfPeopleInStadium</em> probably won&#8217;t suffice.)</li>
  117. <li>Does it have unit tests? Are they good tests?</li>
  118. <li>Does it have integration tests? Are they good tests?</li>
  119. <li>Does it have automation tests? Are they good tests?</li>
  120. <li>If fixing a bug, do the tests properly recreate the original bug?</li>
  121. </ol>
  122. </li>
  123. <li style="text-align: justify;">Is it readable?
  124. <ol>
  125. <li>Does it have any spelling errors and typos?</li>
  126. <li>Does it have any weird acronyms?</li>
  127. <li>For written content, is it understandable and proper? (i.e., proper English or the language that you&#8217;re using for documentation and comments)</li>
  128. <li>Are comments meaningful and useful?</li>
  129. </ol>
  130. </li>
  131. <li style="text-align: justify;">Is the code maintainable?
  132. <ol>
  133. <li>If it introduces any new &#8220;concepts&#8221; to the system, are they documented somewhere?</li>
  134. <li>Does it avoid any weird code constructions? (&#8220;Clever code&#8221;)</li>
  135. <li>Does the naming of variables and methods align with their meaning? (Can you tell what it is based on its title?)</li>
  136. </ol>
  137. </li>
  138. <li>Does it have the right coding style?
  139. <ol>
  140. <li style="text-align: justify;">Variable / method / class casing</li>
  141. <li style="text-align: justify;">Indentation</li>
  142. <li style="text-align: justify;">Bracing</li>
  143. <li style="text-align: justify;">Commenting density</li>
  144. </ol>
  145. </li>
  146. </ol>
  147. <h2>4. Read the PR in the right way</h2>
  148. <p style="text-align: justify;">Depending on the type of project and the nature of the change being introduced, it will make more sense that you go through those changes in a different way. Think of it like this: when you&#8217;re looking for a particular piece of failed code, you usually debug mentally, reading the code from input down to processing, following the logic across its calls. Reading code like this gives you a good idea of the dependencies between components and how they interact, but very likely they cover one or two different scenarios.</p>
  149. <p style="text-align: justify;">Reading the code top to bottom helps you think about the abstractions being made and if they make sense for flexible code that needs to support different scenarios, but they don&#8217;t obviously tell you if the dependencies are redundant or sufficient or even lacking &#8212; at least not right away.</p>
  150. <p style="text-align: justify;">When you look at different modules or namespaces on their own, you&#8217;ll have a good idea about the way that the subsystems interact and their organization, which will help you spot problems in the general design and architecture, but not so much about the details of the implementation.</p>
  151. <p style="text-align: justify;">Make sure to think about this when looking at the reviewed code, and look at it under different eyes and different order. Just make sure to cover it all at least once.</p>
  152. <h2>5. Give feedback in a way that it can be discussed without confusions</h2>
  153. <p style="text-align: justify;">It&#8217;s very likely that you&#8217;ll give several feedback points to the original developer. Furthermore, some items may be quite specific while others may be open-ended or up to discussion. If your feedback is a single block of text, it&#8217;ll be difficult to engage in a conversation without confusing different points.</p>
  154. <p style="text-align: justify;">I actually like to take advantage of some service&#8217;s features like GitHub and Bitbucket to comment on particular lines. That way, I provide a comment that is already in some context, and they allow for discussions particular to that one piece of feedback. Even better, if the files are changed (very likely because of the feedback given) the thread is hidden so that the outdated original feedback is not visible interrupting the code flow.</p>
  155. <p style="text-align: justify;">GitHub has now adopted a code-review approach where you queue up all your messages and you send them all together as part of the review, that you can later approve or reject. Similar to what Microsoft did on TFS online. This is useful because you might be leaving comments about the code and later on realize that your comment would be better rephrased or plain wrong. If you do this, make sure to re-read all comments before sending.</p>
  156. <p style="text-align: justify;">If you don&#8217;t use those services, still, following that approach has its benefits. Write down on your own what the concern is and where, and when you finished reviewing the code, go to the relevant part and leave the comment.</p>
  157. <h2>6. Avoid ping-pongs</h2>
  158. <p style="text-align: justify;">Make sure to give the fullest review as possible, instead of providing each piece for the developer to go fix. Regardless if you provide it in a single block of text or in separate comments, try to give them all at the same time so that the developer (and you) can make the best use of both of your times. Coming back with a change to find out that something else needed to be fixed is not going to be funny to the developer and always having something else to check is going to be exhausting to you.</p>
  159. <p style="text-align: justify;">Sometimes people will give code feedback by email. If that&#8217;s the case, structure your email so that they can answer in line and you can start removing parts of the conversation as you reach conclusions.</p>
  160. <h2>7. Be polite</h2>
  161. <p style="text-align: justify;">When providing feedback, do not ever approach that communication from the vision of &#8220;this is wrong&#8221;, even if it is. Always approach it from a &#8220;it could be improved / it should be improved&#8221;. or even as a question, unless you&#8217;re absolutely sure. Remember that, first of all, there&#8217;s a person on the other side, and second, they&#8217;re doing their best. Even if there are technological, knowledge, experience or time limitations that made the developer turn in non-acceptable code, remember that they&#8217;re doing this for the best of the project.</p>
  162. <p style="text-align: justify;"><a href="http://www.theregister.co.uk/2016/07/11/linus_torvalds_in_sweary_rant_about_punctuation_in_kernel_comments/">Linus Torvals-style rants</a> are pretty fun to read, but remember it&#8217;s hurtful to be on the receiving end. Also, why make enemies if you don&#8217;t need to?</p>