# Code Smell 200 - Poltergeist

*An object that appears and disappears mysteriously*

> TL;DR: Add the necessary indirection layers, but no more.

# Problems

- [Accidental](https://maximilianocontieri.com/no-silver-bullet) complexity

- Readability

- YAGNI violation

# Solutions

1. 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

[Gist Url]: # (https://gist.github.com/mcsee/e51b8f319d33c800895b14e463b3b380)
```csharp
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

[Gist Url]: # (https://gist.github.com/mcsee/0c13213cc8d76d0f1d5041deb94a7946)
```csharp
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](https://maximilianocontieri.com/code-smell-67-middle-man) objects if they are not needed. 

# Relations

%[https://maximilianocontieri.com/code-smell-54-anchor-boats]

%[https://maximilianocontieri.com/code-smell-67-middle-man]

# More Info

- [Wikipedia](https://en.wikipedia.org/wiki/Poltergeist_(computer_programming))

# Disclaimer

Code Smells are my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Lan Gao](https://unsplash.com/@langao) on [Unsplash](https://unsplash.com/images/things/ghost)
    
* * *

> 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_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
