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
Break your 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
Code Smell 90 - Implementative Callback Events
Maxi Contieri ・ Oct 7 '21
#javascript
#webdev
#beginners
#tutorial
More info
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
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
#codenewbie
#programming
#quotes
#software
This article is part of the CodeSmell Series.