Practical Exercise 5
Hands-On
Use AI to review code for quality, security, and best practices
2026 WayUp
1
Review code for security vulnerabilities
2
Identify code smells and anti-patterns
3
Check for performance issues
4
Suggest improvements and refactoring
@app.route('/login', methods=['POST'])
def login():
username = request.form['username']
password = request.form['password']
# Check credentials
query = f"SELECT * FROM users WHERE username='{username}' AND password='{password}'"
user = db.execute(query).fetchone()
if user:
session['user_id'] = user['id']
session['role'] = user['role']
return redirect('/dashboard')
return f"Login failed for user: {username}", 401
class OrderProcessor:
def process(self, order_data, user_data, payment_data, shipping_data, discount_code,
notify_email, notify_sms, priority, gift_wrap, gift_message):
# Validate order
if order_data['items'] and len(order_data['items']) > 0:
for item in order_data['items']:
if item['quantity'] > 0:
if item['price'] > 0:
pass # valid
# Calculate total (duplicated in 3 places)
total = 0
for item in order_data['items']:
total += item['price'] * item['quantity']
# Apply discount (same logic in CartService and CheckoutService)
if discount_code == 'SAVE10':
total = total * 0.9
elif discount_code == 'SAVE20':
total = total * 0.8
elif discount_code == 'SAVE30':
total = total * 0.7
# ... 200 more lines
return {"status": "ok", "total": total}
Long parameter list (10 params), Arrow anti-pattern (nested ifs), DRY violation (discount logic), magic strings, God method
def get_user_orders(user_id):
user = User.query.get(user_id)
orders = []
for order in Order.query.all(): # N+1 query problem
if order.user_id == user_id:
order_data = {
'id': order.id,
'items': [],
'total': 0
}
for item in OrderItem.query.filter_by(order_id=order.id): # Another N+1
product = Product.query.get(item.product_id) # Yet another query
order_data['items'].append({
'name': product.name,
'price': product.price,
'quantity': item.quantity
})
order_data['total'] += product.price * item.quantity
orders.append(order_data)
return orders
You've learned AI-assisted code review
Next: Practical 6 - Refactoring Legacy Code
2026 WayUp - way-up.io