Clean Code JavaScript – Object & Class Patterns
Table of Contents
- Encapsulation
- Immutability
- Cohesion
Encapsulation
// ❌ Bad
user.name = 'John';
// ✅ Good
user.rename('John');
Immutability
// ❌ Bad
user.age++;
// ✅ Good
const updatedUser = user.withAge(user.age + 1);
Cohesion
// ❌ Bad
class User {
calculateTax() {}
}
// ✅ Good
class TaxCalculator {
calculate(user) {}
}