Coding Best Practices Guidance — Guided Lab
Scenario:
You’re writing Python code for multiple projects, but you want to ensure it is readable, maintainable, and follows industry best practices. Your AI assistant can analyze your code, suggest improvements, and provide coding standards guidance.
Step 0: Define Your Goal
Example Python function:
def fact(n):
res = 1
for i in range(1,n+1):
res *= i
return res
Goal:
- Improve readability, naming conventions, and structure
- Suggest Pythonic improvements, docstrings, and comments
Step 1: Craft the AI Prompt
Treat AI as a senior developer or code reviewer. Include:
- Code snippet
- Ask for best practice improvements
- Optional: include naming conventions, documentation, and optimization tips
Example Prompt:
Review the following Python function and suggest improvements following coding best practices.
Include naming conventions, docstrings, readability, and any optimizations.
Step 2: AI Output Example
Improved Function:
def factorial(n: int) -> int:
"""
Calculate the factorial of a non-negative integer n.
Parameters:
n (int): A non-negative integer.
Returns:
int: Factorial of n.
Raises:
ValueError: If n is negative.
"""
if n < 0:
raise ValueError("Input must be non-negative")
result = 1
for i in range(2, n+1):
result *= i
return result
Best Practices Applied:
- Clear function name (
factorial) - Type hints (
n: int -> int) - Docstring with parameters, return, and exceptions
- Error handling for invalid input
- Loop starts at 2 to reduce unnecessary multiplication
Step 3: Mini Lab Challenges
- Review
is_prime(n)and ask AI to suggest improvements - Ask AI to apply PEP8 standards across a small script
- Generate suggestions for modularizing repetitive code
- Challenge: Ask AI to recommend logging, testing, and exception handling best practices
Step 4: Pro Tips
- Treat AI as a senior mentor or code reviewer
- Include instructions to follow PEP8, add docstrings, and improve readability
- Use AI feedback to refactor and standardize your codebase
- Iteratively apply recommendations and re-test your code
Lab Summary
- AI can analyze code and suggest best practices for readability, maintainability, and safety
- Clear prompts + code snippet = actionable improvement recommendations
- Combine AI suggestions with personal review for production-quality code
- Using AI for best practices ensures consistent, professional code across projects


