Objects created without arguments are often mutable and erratic
TL;DR: Pass all your essential arguments when creating objects.
Problems
Anemic Models
Solutions
Context
It is common usage using a zero-argument constructor and a bunch of setters to change it.
Beans is a well-known example of this code smell.
Sample Code
Wrong
public Person();
// Anemic and mutable
Right
public Person(String name, int age){
this.name = name;
this.age = age;
}
}
// We 'pass' the essence to the object
// So it does not mutate
Detection
[X] Automatic
We can check all constructors, but there are some false positives.
Stateless objects are a valid example.
Tags
- Mutability
Conclusion
Empty constructors are mutability hints and accidental implementation issues.
We need to research usages to improve our solutions.