Javarevisited
Published in

Javarevisited

Code Smell 147 — Too Many Methods

Util classes are great to gather protocol

TL;DR: Don’t add an 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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Maximiliano Contieri

Maximiliano Contieri

1.5K Followers

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com