DEV Community

Cover image for Code Smell 197 - Gratuitous Context
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

2

Code Smell 197 - Gratuitous Context

This is a nice way to mark 'your' classes and objects

TL;DR: Don prefix or suffix your names with irrelevant information

Problems

  • Lack of Polymorphism

  • Bad Naming

  • Bijection violation with names

Solutions

  1. Remove this context from your names

Context

In software development, gratuitous context refers to the unnecessary inclusion of additional information or data in code or user interfaces that do not contribute to the functionality or usability of the software.

It can make the software more difficult to use, understand and maintain.

It also increases the risk of errors or defects.

Sample Code

Wrong

struct WEBBExoplanet {
    name: String,
    mass: f64, 
    radius: f64, 
    distance: f64, 
    orbital_period: f64, 
}

struct WEBBGalaxy {
    name: String,
    classification: String,
    distance: f64, 
    age: f64,
}
Enter fullscreen mode Exit fullscreen mode

Right

struct Exoplanet {
    name: String,
    mass: f64, 
    radius: f64, 
    distance: f64, 
    orbital_period: f64, 
}

struct Galaxy {
    name: String,
    classification: String,
    distance: f64, 
    age: f64,
}
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Semi-Automatic

We can find command patterns and rename all objects.

Tags

  • Naming

Conclusion

Class Preffixing was a widespread practice decades ago to claim ownership.

Carefully consider the context and content of the software, and avoid including unnecessary or extraneous information wherever possible.

Now we know clean names are more important.

Relations

More Info

Disclaimer

Code Smells are my opinion.

Credits

Photo by Mitchell Griest on Unsplash


The most dangerous kind of waste is the waste we do not recognize.

Shigeo Shingo


This article is part of the CodeSmell Series.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

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

Okay