DEV Community

Cover image for Code Smell 200 - Poltergeist
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

4

Code Smell 200 - Poltergeist

An object that appears and disappears mysteriously

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

Problems

  • Accidental 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

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();
Enter fullscreen mode Exit fullscreen mode

Right

Car porsche = new Car();

porsche.driveCar();
// We don't need the driver
Enter fullscreen mode Exit fullscreen mode

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

Photo by Lan Gao on Unsplash


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.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (2)

Collapse
 
cicirello profile image
Vincent A. Cicirello

That's a fun name for a code smell.

Collapse
 
mcsee profile image
Maxi Contieri

it is not mine :)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay