original source code is a work that stemmed solely from the author, so that everybody is satisfied with the result whereas in the accepted source code the author incorporated the reviewers’ suggestions
+ url = SERVICE_YAML_URL.format(repo, service_name) + headers = { + 'Accept': 'application/ What about using a more precise name, such us find_docs_link? ' find_record NAMING is hard
57 58 59 60 61 + 'Authorization': 'token {}'.format(settings.get('github_token')) + } + response = requests.get(url, headers=headers) + service_yaml = yaml.load(response.text) + doc = response.get('docs') + + if doc is not None: + if link_string_name in doc: + self.attachments = build_attachments(doc[link_string_name]) + return doc[link_string_name] + return "Sorry, but {} doesn't seem to be a document available for {}.".format(link_string_name, service_name) + else: + return "Sorry, but it seems like we can't find the documentation you're looking Assigning attachments to the instance is an unexpected side effect of the method, let’s extract it to a separate place.( one responsibility + self.attachments = build_attachments(doc[link_string_name])
+ if doc is not None: + if link_string_name in doc: + self.attachments = build_attachments(doc[link_string_name]) + return doc[link_string_name] + return "Sorry, but {} doesn't seem to be a document available for {}.".format(link_string_name, service_name) + else: + return "Sorry, but it seems like we can't find the documentation you're looking for." 49 50 51 52 53 54 55 56 57 58 59 60 61 Smaller steps + return doc[link_string_name] + return "Sorry, but {} doesn't seem to be a document available for
59 60 61 headers=headers) + service_yaml = yaml.load(response.text) + doc = response.get('docs') + + if doc is not None: + if link_string_name in doc: + self.attachments = build_attachments(doc[link_string_name]) + return doc[link_string_name] + return "Sorry, but {} doesn't seem to be a document available for {}.".format(link_string_name, service_name) + else: + return "Sorry, but it seems like we can't find the documentation you're looking for." Smaller steps This method is long. Let’s split it into smaller steps: one for fetching the yaml file from Github and one that builds attachments. )
given class/method/function does? ✅ What does the code do? ✅ Does the code have unexpected side effects? ✅ Do functions have one responsibility? ✅ Could we split functions into smaller steps? 6 of 17
attachments = [] + for link in links: + attachments.append({ + 'text': link, + }) + return attachments 120 121 122 123 124 125 126 127 + for link in links: link
self.service, link) + attachments = [] + for link in links: + attachments.append({ + 'text': link, + }) + return attachments + for link in links: 120 121 122 123 124 125 126 127 link
class/method/function does? ✅ Do names reflect what code does? ✅ Does code have unexpected side effects? ✅ Do functions have one responsibility? ✅ Could we split functions into smaller steps? ✅ Have you tried code locally? 7 of 17
class/method/function does? ✅ Do names reflect what code does? ✅ Does code have unexpected side effects? ✅ Do functions have one responsibility? ✅ Could we split functions into smaller steps? ✅ Have you tried code locally? ✅ Are exceptions handled correctly? 8 of 17
class/method/function does? ✅ Do names reflect what code does? ✅ Does code have unexpected side effects? ✅ Do functions have one responsibility? ✅ Could we split functions into smaller steps? ✅ Have you tried code locally? ✅ Are exceptions handled correctly? ✅ Check Python gotchas. 9 of 17
class/method/function does? ✅ Do names reflect what code does? ✅ Does code have unexpected side effects? ✅ Do functions have one responsibility? ✅ Could we split functions into smaller steps? ✅ Have you tried code locally? ✅ Are exceptions handled correctly? ✅ Check Python gotchas. ✅ Edge cases for ifs, fors, whiles, dates. 10 of 17
. day = datetime.date.today() for offset in range(31): day = day + datetime.timedelta(day=offset) if is_working_day(day): break Limit while loops +day = datetime.date.today() +while not is_working_day(day): + day = day + datetime.timedelta(day=1) 43 44 45
class/method/function does? ✅ Do names reflect what code does? ✅ Does code have unexpected side effects? ✅ Do functions have one responsibility? ✅ Could we split functions into smaller steps? ✅ Have you tried code locally? ✅ Are exceptions handled correctly? ✅ Check Python gotchas. ✅ Edge cases for ifs, fors, whiles, dates. ✅ Are there unnecessary while loops? 11 of 17
class/method/function does? ✅ Do names reflect what code does? ✅ Does code have unexpected side effects? ✅ Do functions have one responsibility? ✅ Could we split functions into smaller steps? ✅ Have you tried code locally? ✅ Are exceptions handled correctly? ✅ Check Python gotchas. ✅ Edge cases for ifs, fors, whiles, dates. ✅ Are there unnecessary while loops? ✅ Is every line of code used? 12 of 17
✅ check library’s documentation ✅ check if library choice is consistent with the rest of the codebase ✅ what’s the licence of the library? ✅ is it actively maintained?
party libraries +response = json.loads(response.text) requests has a method that will return JSON for us, so could rewrite this line to 1 response = response.json()
class/method/function does? ✅ Do names reflect what code does? ✅ Does code have unexpected side effects? ✅ Do functions have one responsibility? ✅ Could we split functions into smaller steps? ✅ Have you tried code locally? ✅ Are exceptions handled correctly? ✅ Check Python gotchas. ✅ Edge cases for ifs, fors, whiles, dates. ✅ Are there unnecessary while loops? ✅ Is every line of code used?
names reflect what code does? ✅ Does code have unexpected side effects? ✅ Do functions have one responsibility? ✅ Could we split functions into smaller steps? ✅ Have you tried code locally? ✅ Are exceptions handled correctly? ✅ Check Python gotchas. ✅ Edge cases for ifs, fors, whiles, dates. ✅ Are there unnecessary while loops? ✅ Is every line of code used? ✅ Check usage of third party libraries. 13 of 17
class/method/function does? ✅ Do names reflect what code does? ✅ Does code have unexpected side effects? ✅ Do functions have one responsibility? ✅ Could we split functions into smaller steps? ✅ Have you tried code locally? ✅ Are exceptions handled correctly? ✅ Check Python gotchas. ✅ Edge cases for ifs, fors, whiles, dates. ✅ Are there unnecessary while loops? ✅ Is every line of code used? ✅ Check usage of third party libraries.
have one responsibility? ✅ Could we split functions into smaller steps? ✅ Have you tried code locally? ✅ Are exceptions handled correctly? ✅ Check Python gotchas. ✅ Edge cases for ifs, fors, whiles, dates. ✅ Are there unnecessary while loops? ✅ Is every line of code used? ✅ Check usage of third party libraries. ✅ Is solution performant? ✅ Does optimisation hurt code readability? 15 of 17
functions into smaller steps? ✅ Have you tried code locally? ✅ Are exceptions handled correctly? ✅ Check Python gotchas. ✅ Edge cases for ifs, fors, whiles, dates. ✅ Are there unnecessary while loops? ✅ Is every line of code used? ✅ Check usage of third party libraries. ✅ Is solution performant? ✅ Does optimisation hurt code readability? ✅ Look for vulnerabilities. 16 of 17
you tried code locally? ✅ Are exceptions handled correctly? ✅ Check Python gotchas. ✅ Edge cases for ifs, fors, whiles, dates. ✅ Are there unnecessary while loops? ✅ Is every line of code used? ✅ Check usage of third party libraries. ✅ Is solution performant? ✅ Does optimisation hurt code readability? ✅ Look for vulnerabilities. ✅ What is missing (tests, docs, metrics, logs, etc.)? 17 of 17
Which Problems Do They Fix? There is a mismatch between the expectations and the actual outcomes of code reviews. “ ” [...] review does not result in identifying defects as often as project members would like and even more rarely detects deep, subtle, or “macro” level issues. Relying on code review in this way for quality assurance may be fraught.
Code review can be used to improve code style, find alternative solutions, increase learning, share code ownership, etc.. This should guide code review policies. A. Bacchelli, C. Bird, Modern Code Reviews in Open-Source Projects: Which Problems Do They Fix?
fix all the issues in their pull request. 8 Mind your language! 2 Understand business/product side of the code. 3 Review code of people more experienced than you. beyond the code ✂ Encourage smaller pull requests. 7 Discuss alternative solutions first.