Avoid Using the Prefix "Collection" on Properties
TL;DR: Drop "collection" prefix for clarity.
When you prefix properties with terms like "collection," you introduce redundancy and verbosity into your code.
This makes your code harder to read and maintain and adds unnecessary complexity.
Coupling the name to a collection implementation prevents you from introducing a proxy or middle object to manage the relation.
struct Task {
collection_of_subtasks: Vec<Subtask>,
subtasks_collection: Vec<Subtask>,
}
impl Task {
fn add_subtask(&mut self, subtask: Subtask) {
self.collection_of_subtasks.push(subtask);
self.subtasks_collection.push(subtask);
}
}
struct Task {
subtasks: Vec<Subtask>,
}
impl Task {
fn add_subtask(&mut self, subtask: Subtask) {
self.subtasks.push(subtask);
}
}
You can add rules to your linter preventing these redundant names.
AI code generators produce this smell if they try to over-describe property names.
They tend to generate overly verbose names to be explicit, which can lead to redundancy.
AI tools can fix this smell if you instruct them to simplify property names. They can refactor your code to use more concise and clear names.
Simplifying property names by removing prefixes like "collection" leads to more readable and maintainable code.
It would be best to focus on clear, direct names that communicate the purpose without redundancy.
Code Smell 38 - Abstract Names
Code Smell 171 - Plural Classes
What exactly is a name - Part II Rehab
Code Smells are my opinion.
Photo by Karen Vardazaryan on Unsplash
Good design adds value faster than it adds cost.
Thomas C. Gale
Software Engineering Great Quotes
This article is part of the CodeSmell Series.