Yet another global access coupled with laziness.
TL;DR: Don't use static functions. They are global and utilities. Talk to objects instead.
Problems
Coupling
Testability
Protocol Overloading
Cohesion
Solutions
Class Single Responsibility Principle is to create instance. Honor it.
Delegate method to instance.
Create stateless objects. Don't call them helpers.
Examples
Static class methods
Static attributes
Sample Code
Wrong
class DateStringHelper {
static format(date) {
return date.toString('yyyy-MM-dd'); ;
}
}
DateStringHelper.format(new Date());
Right
class DateToStringFormatter {
constructor(date){
this.date = date;
}
englishFormat() {
return this.date.toString('yyyy-MM-dd');
}
}
new DateToStringFormatter(new Date()).englishFormat()
Detection
We can enforce a policy to avoid static methods (all class methods but constructors).