Code Smell 147 - Too Many Methods

Util classes are great to gather protocol

TL;DR: Don't add accidental protocol to your classes

Problems

  • Readability

  • Single Responsibility Violation

  • Bad Cohesion

  • High Coupling

  • Low Reusability

Solutions

  1. Break your class

  2. Extract Class

Related Refactorings

Context

We tend to put a protocol in the first class we find.

That's not a problem.

We just need to refactor.

Sample Code

Wrong

public class MyHelperClass {
  public void print() { }
  public void format() { }
  // ... many methods more

  // ... even more methods 
  public void persist() { }
  public void solveFermiParadox() { }      
}

Right

public class Printer {
  public void print() { }
}

public class DateToStringFormater {
  public void format() { }
}

public class Database {
  public void persist() { }
}

public class RadioTelescope {
  public void solveFermiParadox() { }
}

Detection

[X] Automatic

Most linters count methods and warn us.

Relations

More info

Refactoring Guru

Tags

  • Cohesion

  • Bloaters

Conclusion

Splitting classes and protocol is a good practice to favor small and reusable objects.

Credits

Photo by Marcin Simonides on Unsplash


There is no code so big, twisted, or complex that maintenance can't make it worse.

Gerald M. Weinberg


This article is part of the CodeSmell Series.