Clean Code
Clean Code
Section titled “Clean Code”Introduction
Section titled “Introduction”Clean code is code that is easy to read, understand, and maintain. It’s the most important skill for professional developers.
Naming
Section titled “Naming”// ❌ Badfunction d(a, b) { return a - b; }const x = ['Alice', 'Bob'];
// ✅ Goodfunction calculateDifference(a, b) { return a - b; }const userNames = ['Alice', 'Bob'];Functions
Section titled “Functions”// ❌ Bad — too many responsibilitiesfunction processUser(user) { // validate // save to DB // send email // log}
// ✅ Good — single responsibilityfunction validateUser(user) { /* ... */ }function saveUser(user) { /* ... */ }function sendWelcomeEmail(user) { /* ... */ }Avoid Deep Nesting
Section titled “Avoid Deep Nesting”// ❌ Badfunction process(order) { if (order) { if (order.isValid) { if (order.paid) { // process } } }}
// ✅ Good — guard clausesfunction process(order) { if (!order) return; if (!order.isValid) return; if (!order.paid) return; // process}Summary
Section titled “Summary”- Meaningful names for variables and functions
- Functions should do one thing (single responsibility)
- Use guard clauses to reduce nesting
- Keep functions small (under 20 lines ideally)
- Comments explain WHY, not WHAT