An object that appears and disappears mysteriously
TL;DR: Add the necessary indirection layers, but no more.
Problems
Accidental complexity
Readability
YAGNI violation
Solutions
- Remove the intermediate object
Context
A poltergeist (or gypsy wagon) is a short-lived object used to perform initialization or to invoke methods in another, more permanent class.
An object is responsible for many small tasks, resulting in excessive coupling and a lack of cohesion in the code.
Sample Code
Wrong
public class Driver
{
private Car car;
public Driver(Car car)
{
this.car = car;
}
public void DriveCar()
{
car.driveCar();
}
}
Car porsche = new Car();
Driver homer = new Driver(porsche);
homer.DriveCar();
Right
Car porsche = new Car();
porsche.driveCar();
// We don't need the driver