Activates when reviewing code to identify quality issues, security vulnerabilities, and suggest improvements
This skill helps conduct thorough code reviews by analyzing code structure, identifying potential issues, and suggesting improvements. It focuses on code quality, security, performance, and maintainability.
When reviewing code, follow these steps:
Input:
def get_user(user_id):
query = f"SELECT * FROM users WHERE id = {user_id}"
return db.execute(query)
Output:
⚠️ Security Issue: SQL Injection Vulnerability
The current code is vulnerable to SQL injection attacks because it uses string formatting to build the SQL query.
Recommended fix:
```python
def get_user(user_id):
query = "SELECT * FROM users WHERE id = ?"
return db.execute(query, (user_id,))
Input:
function calc(a, b, op) {
if (op == 'add') return a + b;
if (op == 'sub') return a - b;
if (op == 'mul') return a * b;
if (op == 'div') return a / b;
}
Output:
💡 Improvement: Use more descriptive names and handle edge cases
Suggested refactoring:
```javascript
function calculate(operand1, operand2, operation) {
const operations = {
'add': (a, b) => a + b,
'subtract': (a, b) => a - b,
'multiply': (a, b) => a * b,
'divide': (a, b) => {
if (b === 0) throw new Error('Division by zero');
return a / b;
}
};
if (!operations[operation]) {
throw new Error(`Unknown operation: ${operation}`);
}
return operations[operation](operand1, operand2);
}