Clean Code Principles for JavaScript Developers
Write maintainable, readable JavaScript code that your future self (and teammates) will thank you for.
Writing code is easy. Writing code that others (including future you) can understand and maintain is hard. Let's change that.
Why Clean Code Matters
Bad code compounds. What starts as "I'll fix it later" becomes technical debt that slows down your entire team. Clean code is an investment that pays dividends.
1. Meaningful Names
Names should reveal intent:
// ❌ Crypticconst d = new Date();const u = users.filter(u => u.a);
// ✅ Clearconst currentDate = new Date();const activeUsers = users.filter(user => user.isActive);Rules:
- Use searchable names
- Avoid mental mapping (i, j, k for non-loops)
- One word per concept (don't mix get/fetch/retrieve)
2. Functions Should Do One Thing
// ❌ Doing too muchfunction saveUserAndSendEmail(userData) { const user = db.users.create(userData); const emailBody = generateEmailBody(user); sendEmail(user.email, emailBody); logActivity('user_created', user.id); updateAnalytics('new_user'); return user;}
// ✅ Single responsibilityfunction createUser(userData) { return db.users.create(userData);}
function notifyNewUser(user) { const emailBody = generateWelcomeEmail(user); sendEmail(user.email, emailBody);}3. Keep Functions Small
Rule of thumb: If you need to scroll, it's too long.
// ✅ Small, focused functionsfunction validateUserInput(data) { if (!data.email) throw new Error('Email required'); if (!isValidEmail(data.email)) throw new Error('Invalid email'); if (data.password.length < 8) throw new Error('Password too short');}
function createUser(data) { validateUserInput(data); const hashedPassword = hashPassword(data.password); return db.users.create({ ...data, password: hashedPassword });}4. Avoid Boolean Parameters
Boolean flags indicate a function does multiple things:
// ❌ Confusingfunction createUser(userData, sendEmail) { const user = db.users.create(userData); if (sendEmail) { sendWelcomeEmail(user); } return user;}
// ✅ Clear intentfunction createUser(userData) { return db.users.create(userData);}
function createUserWithWelcomeEmail(userData) { const user = createUser(userData); sendWelcomeEmail(user); return user;}5. Use Object Destructuring
Make parameter expectations clear:
// ❌ Position-dependentfunction createPost(title, content, author, tags, published) { // ...}createPost('Title', 'Content', 'John', ['js'], true);
// ✅ Self-documentingfunction createPost({ title, content, author, tags = [], published = false }) { // ...}createPost({ title: 'Title', content: 'Content', author: 'John', tags: ['js'], published: true});6. Early Returns
Reduce nesting with guard clauses:
// ❌ Deep nestingfunction processUser(user) { if (user) { if (user.isActive) { if (user.hasPermission) { // do something } } }}
// ✅ Early returnsfunction processUser(user) { if (!user) return; if (!user.isActive) return; if (!user.hasPermission) return; // do something}7. Avoid Magic Numbers
// ❌ What does 86400000 mean?setTimeout(cleanup, 86400000);
// ✅ Clear and maintainableconst ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;setTimeout(cleanup, ONE_DAY_IN_MS);8. Don't Comment Bad Code, Rewrite It
// ❌ Comment explaining bad code// Check if user is active and has permissionif (u.a && u.p) { // ...}
// ✅ Self-explanatory codeconst canAccess = user.isActive && user.hasPermission;if (canAccess) { // ...}9. Error Handling
Handle errors properly:
// ❌ Silent failurefunction getUser(id) { try { return db.users.find(id); } catch (error) { console.log(error); }}
// ✅ Proper error handlingasync function getUser(id) { try { return await db.users.find(id); } catch (error) { logger.error('Failed to fetch user', { id, error }); throw new DatabaseError(`Could not find user ${id}`); }}10. Use Modern JavaScript Features
// ❌ Old stylevar result = [];for (var i = 0; i < items.length; i++) { if (items[i].active) { result.push(items[i].name); }}
// ✅ Modern and readableconst result = items .filter(item => item.active) .map(item => item.name);11. Consistent Code Style
Use a linter and formatter:
// .eslintrc.json{ "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], "rules": { "no-console": "warn", "no-unused-vars": "error" }}// .prettierrc{ "semi": true, "singleQuote": true, "tabWidth": 2, "trailingComma": "es5"}12. Pure Functions When Possible
// ❌ Impure (side effects)let total = 0;function addToTotal(amount) { total += amount; return total;}
// ✅ Pure (predictable)function add(a, b) { return a + b;}The Boy Scout Rule
"Leave the code cleaner than you found it."
Even small improvements add up:
- Rename a confusing variable
- Extract a complex condition
- Add a missing type annotation
- Remove unused code
Recommended Tools
- ESLint - Catch bugs and enforce rules
- Prettier - Consistent formatting
- TypeScript - Type safety
- SonarQube - Code quality metrics
- Husky - Pre-commit hooks
Conclusion
Clean code isn't about perfection - it's about clarity. Every time you write code, ask yourself: "Will someone else understand this in 6 months?"
If the answer is no, refactor it.
Remember: Code is read far more often than it's written. Optimize for readability.