Review Training Snippets¶
This assignment provides a set of short code samples with intentional flaws. Use them to practice identifying bugs, style violations, and risky patterns during peer review.
How to Use These Snippets¶
- Read each code block carefully.
- Make notes about any issues you observe, referencing the Code Style Guide where applicable.
- Suggest specific fixes or improvements as if you were leaving comments in a pull request.
A simple workflow is illustrated below:
flowchart TD
A[Clone repo] --> B[Review code]
B --> C[Document findings]
C --> D[Submit feedback]
Snippet 1 – Unhandled Edge Case¶
def average(values):
total = 0
for v in values:
total += v
return total / len(values) # fails when values is empty
Missing docstring and no input validation.
Snippet 2 – Resource Leak¶
class DataReader:
def __init__(self, path):
self.path = path
self.cache = None
def read(self):
if self.cache is not None:
return self.cache
f = open(self.path)
data = f.read()
self.cache = data.splitlines()
return self.cache
File handle never closed and a mutable default is mishandled.
Snippet 3 – Dead Code¶
def read_file(file_path):
with open(file_path) as f:
data = f.read()
return data
print("Finished reading") # unreachable line
The print statement will never execute.
Snippet 4 – Style Violations¶
def CountItems(Items):
Count=0
for item in Items:
Count = Count+1
return Count
Improper naming, inconsistent indentation, and spacing issues.
Practice reviewing these samples until you can quickly spot mistakes and explain how to fix them.