Code Review Assistance — Guided Lab
Scenario:
You just wrote a Python function for your project, but you’re unsure if it’s efficient, readable, and bug-free. Instead of asking a human mentor, you bring in your AI assistant:
“Review my code and suggest improvements — readability, performance, and best practices.”
Step 1: Craft the AI Prompt
Treat the AI like a mentor or senior developer. Include:
- Your code
- What you want feedback on
- Optional: suggestions for optimization or documentation
Example Prompt:
Here is my Python function:
def factorial(n):
result = 1
for i in range(2, n+1):
result *= i
return result
Please review it. Suggest improvements for readability, efficiency, and error handling. Include comments or sample improved code.
Step 2: AI Output Example
Feedback:
- Add input validation to handle negative numbers
- Include type hints and docstring
- Optionally use recursion or more Pythonic style
Improved Function Example:
def factorial(n: int) -> int:
"""Return the factorial of a non-negative integer n."""
if n < 0:
raise ValueError("Input must be non-negative")
result = 1
for i in range(2, n+1):
result *= i
return result
Explanation:
- Added type hints and docstring → improves readability
- Added error handling → prevents invalid input
- Loop style kept for efficiency and clarity
Step 3: Test & Compare
print(factorial(5)) # 120
print(factorial(0)) # 1
# print(factorial(-1)) # Raises ValueError
- Compare your original code with AI-suggested improvements
- Notice the clarity, safety, and Pythonic practices
Step 4: Mini Lab Challenges
- Submit a
reverse_string(s)function to AI and ask for improvements. - Provide a longer script (e.g., CSV automation) and request efficiency improvements.
- Ask AI to comment every line of your code like a teaching assistant.
- Challenge: let AI suggest one alternative approach and explain trade-offs.
Step 5: Pro Tips
- Ask AI to act as a senior developer or mentor for more advanced feedback
- Request readability improvements, performance suggestions, or security checks
- Use AI to explain trade-offs between different approaches
- Combine AI suggestions with your judgment — don’t blindly copy
Lab Summary
- AI can be your instant code reviewer, giving feedback on readability, efficiency, and best practices
- Treat AI like a mentor: ask for explanations, suggestions, and alternative approaches
- Iteratively improve code: submit → review → modify → test
- Code review assistance speeds up learning and reduces bugs in your projects


