No-Parameterized constructors are a code smell of an **invalid* object that will dangerously mutate.
Incomplete objects cause lots of issues.*
TL;DR: Pass the essence to all your objects so they will not need to mutate.
Problems
Mutability
Incomplete objects
Concurrency inconsistencies between creation and essence setting.
Setters
Nude Models — Part I : Setters
Maxi Contieri ・ Feb 20 '21 ・ 6 min read
#codenewbie
#tutorial
#oop
#programming
Solutions
Pass the object's essence on creation
Create objects with their immutable essence.
The evil powers of mutants
Maxi Contieri ・ Dec 19 '20 ・ 6 min read
#webdev
#programming
#mutation
#oop
Examples
- Some persistence frameworks in static typed languages require an empty constructor.
Exceptions
- Stateless objects. Always better solution than static class methods.
Sample Code
Wrong
class AirTicket {
constructor() {
}
}
Right
class AirTicket {
constructor(origin, destination, arline, departureTime, passenger) {
//...
}
}
Detection
Any linter can warn this (possible) situation.