Skip to main content

Code reviews rule: Excessive number of methods in class

Written by David Martin
Updated this week

Excessive number of methods in class

Why is this an issue?

Classes with too many methods are difficult to understand and maintain. Large classes often indicate:

  • Violation of single responsibility: The class is doing too many things

  • Missing abstractions: Related methods should be extracted to separate classes

  • Technical debt: Methods accumulated over time without refactoring

Examples

Example of incorrect code (too many methods):

public class AccountService {
public void createAccount() { }
public void updateAccount() { }
public void deleteAccount() { }
public void validateAccount() { }
public void calculateRevenue() { }
public void sendNotification() { }
public void syncToExternalSystem() { }
// ... 40+ more methods
}

Example of correct code (split into focused classes):

public class AccountService {
public void createAccount() { }
public void updateAccount() { }
public void deleteAccount() { }
}

public class AccountValidator {
public void validateAccount() { }
}

public class AccountRevenueCalculator {
public Decimal calculateRevenue() { }
}

How can I fix violations?

  1. Split the class: Extract related methods into focused classes.

  2. Use composition: Create smaller classes and compose them together.

  3. Apply design patterns: Use patterns like Strategy or Command to distribute behavior.

  4. Remove unused methods: Delete methods that are no longer needed.

Example refactoring:

// Before: One large class
public class AccountService {
// 50+ methods covering validation, calculations, integrations...
}

// After: Focused classes
public class AccountValidator { }
public class AccountCalculator { }
public class AccountIntegrationService { }

When should I disable this rule?

You may dismiss specific violations for utility classes that intentionally group many related helper methods.

Resources

Did this answer your question?