Photo by Mitchell Griest on Unsplash
Code Smell 197 - Gratuitous Context
This is a nice way to mark 'your' classes and objects
TL;DR: Don't prefix or suffix your names with irrelevant information
Problems
Lack of Polymorphism
Bad Naming
Bijection violation with names
Solutions
- 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,
}
Right
struct Exoplanet {
name: String,
mass: f64,
radius: f64,
distance: f64,
orbital_period: f64,
}
struct Galaxy {
name: String,
classification: String,
distance: f64,
age: f64,
}
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.