Core Philosophy: Modular, Functional, Maintainable Golden Rule: If you can't easily test it, refactor it
Critical Patterns (use these):
Anti-Patterns (avoid these):
Modular: Everything is a component - small, focused, reusable Functional: Pure functions, immutability, composition over inheritance Maintainable: Self-documenting, testable, predictable
component/
├── index.js # Public interface
├── core.js # Core logic (pure functions)
├── utils.js # Helpers
└── tests/ # Tests
// ✅ Pure
const add = (a, b) => a + b;
const formatUser = (user) => ({ ...user, fullName: `${user.firstName} ${user.lastName}` });
// ❌ Impure (side effects)
let total = 0;
const addToTotal = (value) => { total += value; return total; };
// ✅ Immutable
const addItem = (items, item) => [...items, item];
const updateUser = (user, changes) => ({ ...user, ...changes });
// ❌ Mutable
const addItem = (items, item) => { items.push(item); return items; };
// ✅ Compose small functions
const processUser = pipe(validateUser, enrichUserData, saveUser);
const isValidEmail = (email) => validateEmail(normalizeEmail(email));
// ❌ Deep inheritance
class ExtendedUserManagerWithValidation extends UserManager { }
// ✅ Declarative
const activeUsers = users.filter(u => u.isActive).map(u => u.name);
// ❌ Imperative
const names = [];
for (let i = 0; i < users.length; i++) {
if (users[i].isActive) names.push(users[i].name);
}
// ✅ Explicit error handling
function parseJSON(text) {
try {
return { success: true, data: JSON.parse(text) };
} catch (error) {
return { success: false, error: error.message };
}
}
// ✅ Validate at boundaries
function createUser(userData) {
const validation = validateUserData(userData);
if (!validation.isValid) {
return { success: false, errors: validation.errors };
}
return { success: true, user: saveUser(userData) };
}
// ✅ Dependencies explicit
function createUserService(database, logger) {
return {
createUser: (userData) => {
logger.info('Creating user');
return database.insert('users', userData);
}
};
}
// ❌ Hidden dependencies
import db from './database.js';
function createUser(userData) { return db.insert('users', userData); }
❌ Mutation: Modifying data in place ❌ Side effects: console.log, API calls in pure functions ❌ Deep nesting: Use early returns instead ❌ God modules: Split into focused modules ❌ Global state: Pass dependencies explicitly ❌ Large functions: Keep < 50 lines
✅ Pure functions whenever possible ✅ Immutable data structures ✅ Small, focused functions (< 50 lines) ✅ Compose small functions into larger ones ✅ Explicit dependencies (dependency injection) ✅ Validate at boundaries ✅ Self-documenting code ✅ Test in isolation
Golden Rule: If you can't easily test it, refactor it.