
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
Detection
[X] Manual
This is a design smell.
Tags
- Complexity
Conclusion
Don't add accidental complexity to the essential complexity we already have.
Remove middleman objects if they are not needed.
Relations
More Info
Disclaimer
Code Smells are my opinion.
Credits
The art of programming is the art of organizing complexity, of mastering multitude and avoiding its bastard chaos as effectively as possible.
E. W. Dijkstra
This article is part of the CodeSmell Series.
About the author
I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.
Comments
Join the discussionI Like the blog Maxi Contieri
Code Smells
In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.





