# Large Class

> Classes that have grown too large and handle too many responsibilities.

- **Category**: Code Smells
- **Subcategory**: bloaters
- **Canonical URL**: https://designpattern.fyi/code_smells/large-class/

---

## Description
'**Intent**: Identify classes that have become too large and complex, violating Single Responsibility Principle and becoming difficult to maintain.

**Context**: You have classes with hundreds of lines, many methods, and multiple responsibilities. Understanding the class requires significant effort, and changes risk breaking unrelated functionality.

**Solution**: Break down large classes into smaller, focused classes. Extract related methods and fields into separate classes. Apply Extract Class and Single Responsibility Principle.'



## Use Cases
Use when classes exceed a few hundred lines, when they handle multiple concerns, or when they're difficult to understand and modify.



## Implementation Example

```javascript
// Before: Large class
class User {
  constructor(name, email, password) {
    this.name = name;
    this.email = email;
    this.password = password;
    this.orders = [];
    this.addresses = [];
    this.preferences = {};
    this.notifications = [];
  }

  validateEmail() { /* ... */ }
  validatePassword() { /* ... */ }
  hashPassword() { /* ... */ }
  addOrder(order) { /* ... */ }
  getOrders() { /* ... */ }
  addAddress(address) { /* ... */ }
  getAddresses() { /* ... */ }
  updatePreferences(prefs) { /* ... */ }
  sendNotification(notification) { /* ... */ }
  // ... many more methods
}
```



## Trade-offs


### Advantages

- Focused, single-purpose classes

- Easier to understand and maintain

- Better adherence to SRP




### Considerations & Drawbacks

- Can increase number of classes

- May require careful coordination







---
**Reference**: [Original Source](https://refactoring.guru/smells/large-class)

