A skill that improves code structure without altering behavior, prioritizing clarity and maintainability.
To refactor code for enhanced readability, reduced complexity, and better maintainability—all while preserving the original functionality. This skill helps developers clean up legacy or verbose code by applying industry-standard best practices.
Before:
def f(d):
r = []
for i in range(len(d)):
if d[i] > 0:
r.append(d[i] * 2)
return r
After:
def double_positive_values(data):
"""Return a list of doubled positive values from the input data."""
return [value * 2 for value in data if value > 0]
Explanation: Renamed f to double_positive_values to describe intent. Used list comprehension to reduce verbosity and improve readability.
Before:
def process_user(user):
if user is not None:
if user.is_active:
if user.has_permission('edit'):
# ... actual logic
return True
return False
After:
def process_user(user):
"""Process user if they exist, are active, and have edit permission."""
if user is None:
return False
if not user.is_active:
return False
if not user.has_permission('edit'):
return False
# ... actual logic
return True
Explanation: Used early returns (guard clauses) to flatten nested conditionals, making the main logic more prominent.
Before:
function handleOrder(order) {
// Validate order
if (!order.items || order.items.length === 0) throw new Error('No items');
if (!order.customer) throw new Error('No customer');
// Calculate total
let total = 0;
for (const item of order.items) {
total += item.price * item.quantity;
}
// Apply discount
if (order.discountCode === 'SAVE10') total *= 0.9;
return total;
}
After:
function validateOrder(order) {
if (!order.items || order.items.length === 0) {
throw new Error('No items');
}
if (!order.customer) {
throw new Error('No customer');
}
}
function calculateSubtotal(items) {
return items.reduce((sum, item) => sum + item.price * item.quantity, 0);
}
function applyDiscount(total, discountCode) {
const discounts = { 'SAVE10': 0.9 };
return discounts[discountCode] ? total * discounts[discountCode] : total;
}
function handleOrder(order) {
validateOrder(order);
const subtotal = calculateSubtotal(order.items);
return applyDiscount(subtotal, order.discountCode);
}
Explanation: Extracted validation, calculation, and discount logic into separate functions, making handleOrder a clear orchestrator of smaller, testable units.