| name | clean-code |
| description | Code quality principles and best practices |
| triggers | ["context","code","quality","review"] |
Clean Code Skill
Purpose: Write maintainable, readable, and professional code
Overview
This skill applies Robert C. Martin's Clean Code principles and industry best practices for writing professional-grade code.
Naming Conventions
Variables
const d = new Date();
const u = users.filter((x) => x.active);
const currentDate = new Date();
const activeUsers = users.filter((user) => user.isActive);
Functions
function process(data) { ... }
function handle(x) { ... }
function validateUserInput(formData) { ... }
function calculateTotalPrice(orderItems) { ... }
Classes
class Data { ... }
class Manager { ... }
class UserRepository { ... }
class OrderService { ... }
Functions
Single Responsibility
function processOrder(order) {
validateOrder(order);
calculateTotal(order);
saveToDatabase(order);
sendEmail(order);
}
function validateOrder(order) { ... }
function calculateOrderTotal(order) { ... }
function persistOrder(order) { ... }
function notifyCustomer(order) { ... }
Keep Small (≤20 lines)
- Each function should do ONE thing
- Extract helper functions when growing
Minimize Parameters
function createUser(name, email, age, address, phone, role) { ... }
function createUser(userData: CreateUserDto) { ... }
Error Handling
try {
doSomething();
} catch (e) {
console.log(e);
}
try {
await processPayment(order);
} catch (error) {
logger.error("Payment processing failed", { orderId: order.id, error });
throw new PaymentFailedError(order.id, error.message);
}
Comments
counter++;
const displayIndex = arrayIndex + 1;
Quick Reference
| Principle | Guideline |
|---|
| Names | Intention-revealing |
| Functions | Small, single purpose |
| Parameters | ≤3 ideally |
| Comments | Explain why, not what |
| Formatting | Consistent, readable |
| Error Handling | Explicit, informative |
| DRY | Don't repeat yourself |
| KISS | Keep it simple |