Code Smell 147 - Too Many Methods

Photo by Marcin Simonides on Unsplash

Code Smell 147 - Too Many Methods

Maxi Contieri
·Jul 6, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Problems
  • Solutions
  • Related Refactorings
  • Context
  • Sample Code
  • Detection
  • Relations
  • More info
  • Tags
  • Conclusion
  • Credits

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.

 
Share this

Impressum

Technical Opinions are my own. I don't have the revealed truth.

Software Design is a creative activity. These are hints and not rigid rules.

I write on BackEnd Business Systems and OOP Design. My advice/experience might not suit other systems.

You can write me at info(at)maximilianocontieri.com