Skip to main content

Command Palette

Search for a command to run...

Code Smell 147 - Too Many Methods

Published
2 min read
Code Smell 147 - Too Many Methods
M

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.

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.

Code Smells

Part 1 of 50

In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.