This story on HackerNoon has a decentralized backup on Sia.
Transaction ID: wXk4fX8l8IMTO0mk3WVHYZS3XvNz8YNA744DW8SxQ8E
Cover

Code Smell 257 - Simplifying Property Names By Removing Prefixes

Written by @mcsee | Published on 2024/7/9

TL;DR
Avoid Using the Prefix "Collection" on Properties. Use Simple Names. Remove 'collection' from the name. Use plural names without the word 'collection.' 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. Good design adds value faster than it adds cost.

Avoid Using the Prefix "Collection" on Properties

TL;DR: Drop "collection" prefix for clarity.

Problems

  • Redundant Naming
  • Verbose Code
  • Reduced Readability
  • Refactoring Challenges
  • Coupled to implementation

Solutions

  1. Use Simple Names
  2. Remove 'collection' from the name
  3. Use plural names without the word 'collection.'

Context

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.

Sample Code

Wrong

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);
    }
}

Right

struct Task {
    subtasks: Vec<Subtask>,
}

impl Task {
    fn add_subtask(&mut self, subtask: Subtask) {
        self.subtasks.push(subtask);
    }
}

Detection

  • [x]Automatic

You can add rules to your linter preventing these redundant names.

Tags

  • Naming

Level

  • [x]Beginner

AI Generation

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 Detection

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.

Conclusion

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.

Relations

Code Smell 38 - Abstract Names

Code Smell 171 - Plural Classes

Code Smell 113 - Data Naming

More Info

What exactly is a name - Part II Rehab

Disclaimer

Code Smells are my opinion.

Credits

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.

How to Find the Stinky Parts of your Code

[story continues]


Written by
@mcsee
I’m a sr software engineer specialized in Clean Code, Design and TDD Book "Clean Code Cookbook" 500+ articles written

Topics and
tags
programming|clean-code|code-smells|software-development|software-engineering|refactoring|refactor-legacy-code|code-smell-series
This story on HackerNoon has a decentralized backup on Sia.
Transaction ID: wXk4fX8l8IMTO0mk3WVHYZS3XvNz8YNA744DW8SxQ8E